Skip to content

Opaque types don't play nicely with anonymous functions #5481

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

Closed
adamgfraser opened this issue Nov 20, 2018 · 0 comments
Closed

Opaque types don't play nicely with anonymous functions #5481

adamgfraser opened this issue Nov 20, 2018 · 0 comments

Comments

@adamgfraser
Copy link
Contributor

Normally I could write an anonymous function and the argument type could be inferred based on the expected type. For example, if I defined type Set[A] = A => Boolean then I could write def singleton[A](a: A): Set[A] = _ == a and the type of _ would be inferred as A. However, if I define Set as opaque and try to do this in the type companion it doesn't work. I have to specify the type of the argument explicitly, which seems unnecessary and inconsistent given that within the type companion the opaque type and its definition are supposed to be the same thing.

object TypeAlias {

  type Set[A] = A => Boolean

  object Set {
    def singleton[A](a: A): Set[A] = _ == a       // Works
  }
}

object OpaqueType {

  opaque type Set[A] = A => Boolean

  object Set {
    //def singleton[A](a: A): Set[A] = _ == a     // Does not compile
    def singleton0[A](a: A): Set[A] = (_: A) == a // Works
  }
}
odersky added a commit to dotty-staging/dotty that referenced this issue Nov 20, 2018
…functions

If the prototype of a function value is a synthetic opaque type alias, assume
the alias type. If the alias type is a function type, this allows one to infer
the parameter types of the function value.
odersky added a commit that referenced this issue Nov 25, 2018
Fix #5481: Follow opaque aliases in prototypes of functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant