You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
}
The text was updated successfully, but these errors were encountered:
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.
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 writedef singleton[A](a: A): Set[A] = _ == a
and the type of_
would be inferred asA
. However, if I defineSet
asopaque
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.The text was updated successfully, but these errors were encountered: