Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic parameter when copying a trait method into an object that has a generic parameter of the same name #4463

Open
tetotechy opened this issue Oct 9, 2023 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@tetotechy
Copy link

This code won't compile:

actor Main
  new create(env: Env) =>
    env.out.print("Hello")
    
trait Foo[In, Out]
  fun apply(src: In^): Out^
  
  fun ref into[Into](from: {(Out^): Into^}): Foo[In, Into] =>
    Converter[In, Out, Into](this, from)
    
primitive Converter[In, Out, Into]
  fun apply(foo: Foo[In, Out], from: {(Out^): Into^}): Foo[In, Into] =>
    object is Foo[In, Into]
      fun apply(src: In^): Into^ =>
        from(foo(src))
    end

(The reason I am using a Converter primitive and not directly an object literal is #4451)

But this will do, just changing the name of the Into type (and only that) into any other name (here, Dest):

actor Main
  new create(env: Env) =>
    env.out.print("Hello")
    
trait Foo[In, Out]
  fun apply(src: In^): Out^
  
  fun ref into[Into](from: {(Out^): Into^}): Foo[In, Into] =>
    Converter[In, Out, Into](this, from)
    
primitive Converter[In, Out, Dest]
  fun apply(foo: Foo[In, Out], from: {(Out^): Dest^}): Foo[In, Dest] =>
    object is Foo[In, Dest]
      fun apply(src: In^): Dest^ =>
        from(foo(src))
    end
@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Oct 9, 2023
@jemc jemc added bug Something isn't working needs investigation This needs to be looked into before its "ready for work" labels Oct 10, 2023
@jemc
Copy link
Member

jemc commented Oct 10, 2023

I suspect this has something to do with how object literal ASTs get expanded in the expr pass, then "caught up" by running prior passes. I suspect that in the process of catching up, there is confusion about type parameter name scoping.

Needs more investigation to get a more concrete take on what's going on.

@SeanTAllen SeanTAllen added the help wanted Extra attention is needed label Oct 10, 2023
@SeanTAllen SeanTAllen changed the title Name clashes in object literals? Generic parameter when copying a trait method into an object that has a generic parameter of the same name Oct 10, 2023
@SeanTAllen SeanTAllen added enhancement New feature or request and removed bug Something isn't working discuss during sync Should be discussed during an upcoming sync needs investigation This needs to be looked into before its "ready for work" labels Oct 10, 2023
@jemc
Copy link
Member

jemc commented Oct 10, 2023

Discussed further on today's sync call.

This is not due to the object literal expansion as I thought in my previous comment, and it's not quite as obvious as being a bug (though it is super unfortunate). It's a surprising limitation.

In the short term, @SeanTAllen wants to add documentation warning about this limitation.

In the long term, the way to fix this would be to change how we copy trait methods into the type that "inherits" them, to rewrite type parameter names to be hygienic.

That would avoid the clash between the Into type parameter that the object literal is using, and the Into type parameter on the into` function that gets copied in.

@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Oct 10, 2023
@SeanTAllen SeanTAllen removed the discuss during sync Should be discussed during an upcoming sync label Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants