Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using nominal type and structural type in self type annotation https://twitter.com/everpeace/status/973449957347753984
$ 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
@everpeace

This comment has been minimized.

Copy link
Owner Author

commented Mar 13, 2018

Another two solutions:

From xuwei_k: https://twitter.com/xuwei_k/status/973452829326127104

 trait Bar{ self: Foo{ val someInt: Int } => }

From eed3si9n_ja: https://twitter.com/eed3si9n_ja/status/973454124585926656

object Util { type SomeInt = { val someInt: Int } }
trait Bar { self: Util.SomeInt with Foo => }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.