I found simple example:
class Post extends LongKeyedMapper[Post] with IdPK { def getSingleton = Post object title extends MappedText(this) object text extends MappedText(this) object date extends MappedDate(this) } object Post extends Post with LongKeyedMetaMapper[Post] { def getPosts(startAt: Int, count: Int) = { Post.findAll(OrderBy(Post.date, Descending), StartAt(startAt), MaxRows(count)) } def getPostsCount = Post.count }
What does it mean with IdPK
?
Thanks.
On Scala Collections there is usually :+ and +: . Both add an element to the collection. :+ appends +: prepends. A good reminder is, : is where the Collection goes. There is as well colA ++: colB to concat collections, where the : side collection determines the resulting type.
Scala is a statically typed programming language. This means the compiler determines the type of a variable at compile time. Type declaration is a Scala feature that enables us to declare our own types.
There are no ++ or -- operators in Scala (use += or -=) Second, Scala encourages the use of immutable values, and with immutable values you'll have no need for these operators.
The :: and ++ operators are valid method identifiers, not reserved words. The Scala collections library defines methods with these identifiers, which means you can also use them for your own methods.
with
means that the class is using a Trait via mixin.
Post
has the Trait IdPK
(similar to a Java class can implements
an Interface).
See also A Tour of Scala: Mixin Class Composition
While this isn't a direct answer to the original question, it may be useful for future readers. From Wikipedia:
Scala allows to mix in a trait (creating an anonymous type) when creating a new instance of a class.
This means that with
is usable outside of the top line of a class definition. Example:
trait Swim { def swim = println("Swimming!") } class Person val p1 = new Person // A Person who can't swim val p2 = new Person with Swim // A Person who can swim
p2
here has the method swim
available to it, while p1
does not. The real type of p2
is an "anonymous" one, namely Person with Swim
. In fact, with
syntax can be used in any type signature:
def swimThemAll(ps: Seq[Person with Swim]): Unit = { ps.foreach(_.swim) }
EDIT (2016 Oct 12): We've discovered a quirk. The following won't compile:
// each `x` has no swim method def swim(xs: Seq[_ >: Person with Swim]): Unit = { xs.foreach(_.swim) }
Meaning that in terms of lexical precedence, with
binds eagerly. It's _ >: (Person with Swim)
, not (_ >: Person) with Swim
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With