Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala with keyword usage

Tags:

scala

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.

like image 264
den bardadym Avatar asked Jul 20 '11 11:07

den bardadym


People also ask

What does :+ mean in Scala?

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.

What is .type in Scala?

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.

Can you do += in Scala?

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.

Which keywords are not reserved in Scala?

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.


2 Answers

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

like image 66
Jonas Avatar answered Sep 20 '22 14:09

Jonas


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.

like image 42
Colin Woodbury Avatar answered Sep 21 '22 14:09

Colin Woodbury