Last active
March 13, 2018 08:17
-
-
Save everpeace/35ec86a9a67ada8c889d3d76a8180033 to your computer and use it in GitHub Desktop.
using nominal type and structural type in self type annotation https://twitter.com/everpeace/status/973449957347753984
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scala | |
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_152). | |
Type in expressions for evaluation. Or try :help. | |
scala> trait Foo {} | |
defined trait Foo | |
# actually I wanted to write like this. But it doesn't compile | |
# trait Bar { | |
# self: { val someInt: Int } with Foo => | |
# } | |
scala> trait Bar { | |
| self: ({ | |
| type λ = { val someInt: Int } | |
| type μ = λ with Foo | |
| })#μ => | |
| } | |
defined trait Bar | |
scala> class FooBar extends Bar with Foo { | |
| val someInt = 3 | |
| } | |
defined class FooBar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another two solutions:
From xuwei_k: https://twitter.com/xuwei_k/status/973452829326127104
From eed3si9n_ja: https://twitter.com/eed3si9n_ja/status/973454124585926656