Skip to content

Instantly share code, notes, and snippets.

@everpeace
Last active March 13, 2018 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everpeace/35ec86a9a67ada8c889d3d76a8180033 to your computer and use it in GitHub Desktop.
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
$ 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
Copy link
Author

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