score:22
The error is occurring because you have constrained the type of the this
reference (which you have named self
) to be of type Self
. When you say new Self with SelfAware
, this is OK, because that object is of type Self
like you asked. But when you say new X with SelfAware
, there is no evidence that X
is in any way a subtype of Self
.
In your new object of type X with SelfAware
, what would be the type of its self
member? Well, it would not be of type Self
, but of type X
. But you have defined the trait SelfAware
so that self
must be of type Self
, so you get a type error.
score:12
To answer the other half of your question (why does println(s.self)
produce an error?), that is because self
is not a field of SelfAware
. It can be used to define such fields, however:
trait SelfAware { self =>
val me = self
}
class X
val x = new X with SelfAware
println(s.me)
score:21
I also found the answer here: http://markthomas.info/blog/?p=92
Self Types
Ordered can be mixed in to any class; it doesn’t depend on any methods or fields of the class that it is mixed in to. Sometimes it’s useful for a trait to be able to use the fields or methods of a class it is mixed in to, this can be done by specifying a self type for the trait. A self type can be specified for a class or a trait as follows:
trait SpellChecker { self =>
...
}
self within the context of this trait will refer to this. Aliasing this is useful for nested classes or traits where it would otherwise be difficult to access a particular this. The syntax can be extended to specify a lower-bounds on this, when this is done the trait or class can use the features of this lower-bound class, so it can extend or modify its behaviour.
trait SpellChecker { self: RandomAccessSeq[char] =>
...
}
The compiler will check that any class in a hierarchy including SpellChecker is or extends RandomAccessSeq[char], so SpellChecker can now use the fields or methods of RandomAccessSeq[char]
Source: stackoverflow.com
Related Query
- How to use scala trait with `self` reference?
- How to use third party libraries with Scala REPL?
- How to use IntelliJ with Play Framework and Scala
- How to use stackable trait pattern with Akka actors?
- Scala How to use extends with an anonymous class
- How to use Typesafe's Config in Scala with encrypted passwords
- How can I use JMH for Scala benchmarks together with sbt?
- How to use Scala XML with Apache Flink?
- How to use takeWhile with an Iterator in Scala
- How to use Scala ARM with Futures?
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- How to use my classes from Scala worksheet in IntelliJ CE with Scala plugin?
- How to use countDistinct in Scala with Spark?
- How do you use external Scala compiler with IDEA 12?
- How to use Quasar with Scala under sbt?
- How does one use Google Guice's @Inject with Scala / (Play 2.4.x)
- How to use scala 2.10 trunk with sbt 0.11.0? (Unresolved dependencies)
- How to use play-plugins-mailer with Play 2.3 and Scala 2.11?
- How do I use Scala to parse CSV data with empty columns?
- How to reference subclasses of static Java classes with generics in Scala
- How to use a Scala Secure Trait in PlayFramework?
- How do I use nightly builds of Scala 2.9 with maven?
- Scala How to use pattern matching with a non generic LazyList?
- How to implement a trait with a generic case class that creates a dataset in Scala
- Scala trait with generic self type
- How do I create horizontal or vertical struts and glue for use with scala BoxPanel?
- Value and column operations in scala spark, how to use a value left of an operator with spark column?
- How do I use Swing with Scala 2.11 in Eclipse?
- How to use path-dependent types with type classes in Scala
- How to use Scala Cats' Kleisli with Either
More Query from same tag
- How can I run inter-dependent queries alongside a non-DB operation in the same transaction using slick
- How Do I Create Custom Actions in Play! 2.0 Framework?
- Scala - write Windows file paths that contain spaces as string literals
- Scala apply with no parameters
- Shuffling Range in Scala is Odd
- Spark Reading Json
- Playframework 2.2.2 not working on windows 7 with scala
- Why does usage of Foo[T <: Bar] require Foo[_ <: Bar] rather than Foo[_]
- Scala Parse recursive JSON
- How to generate xml report with scoverage maven plugin?
- Scala collection one-to-one mapping?
- Is there a shortcut for Disjunction.fold(identity, identity)?
- Which is the sub-type function in this Scala example?
- Scala Spark, compare two DataFrames and select the value of another column
- Scala JPanel rendering synchronisation
- Scala placeholder syntax for anonymous function
- Instantiate a top level lazy value based on value we get in a method scala
- stringbuilder Scala drop duplicate chars
- Why is Key always 0 when creating map
- Debugging a software verifier written in Scala working as plugin of sbt
- Gatling Verifying Decoded JWT token
- How to run server as part of test?
- Scala - Parallel Processing Formulas in an Array
- Why methods of traits are called when a class has method's implementation
- Why does Scala's withDefaultValue method return an unspecific Map?
- spark-submit on yarn did not distribute jars to nm-local-dir
- Spark key value pairs to string
- How to Resolve 'not found: object anorm' for play 2.4.3
- Explicit Type Conversion in Scala
- Trait self type bound: A with B but not A with C