-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
def in structural type is rejected, but val is accepted #1175
Comments
We have not decided yet to what degree dotty will support this. On Mon, Mar 14, 2016 at 2:48 PM, Samuel Gruetter notifications@github.com
Martin Odersky |
If structural types will not be supported, I think we will need a good migration story for patterns where structural types are used as type classes. For example, consider the type Closeable = {
def close(): Unit
} The implicit class CloseableOps[A <: Closeable](resource: A) {
/**
* Lightweight automatic resource management
*/
def autoClosed: ManagedResource[A]
} The best rewrite I could come up with for this situation was changing trait Closeable[T] {
def close(): Unit
}
object Closeable {
// Note. `extends Closeable[java.io.Closeable]` is not correct because then
// we lose the original type
implicit object ZOSClose extends Closeable[ZipOutputStream] {
override def close(e: ZipOutputStream): Unit = e.close()
}
// ... for every supported type
}
implicit class CloseableOps[A](resource: A)(implicit ev: CanClose[A]) Is there a better approach to rewrite the |
I don't think that the use of structural types here is a good idea, you shouldn't call |
Ouch. I thought @soc had shown that nobody uses structural types! If needed I believe the best strategy would be to just add them back, maybe under Scala-2 mode. Maybe there's some synergy to be had with So, question: Can we write a "native" implementation of Edit: Talking to @sjrd it seems this is non-trivial even on JS. So probably just have a direct InvokeDynamic implementation. |
I agree. In addition, there is a performance penalty to pay at runtime. Structural are in general an anti-pattern, in my opinion. However, there is no denying that structural types are (by far) the most syntactically lightweight solution for this situation with
It would make migration at lot smoother where structural types are used. An alternative solution would be to make the type class rewrite more syntactically lightweight, for example with automatic derivation of implicit instances. This could result in a net win on all dimensions. |
@odersky @soc Structural type is the only language feature we don't support at all on Native at the moment. It is indeed trickier to implement for us, given that both runtime and compiler need to have special support for the new kind of method dispatch. We'll revisit this issue once we have initial support for |
Closing, see #1886 |
dotty rejects this example:
scalac accepts it, and I guess dotty should as well?
The text was updated successfully, but these errors were encountered: