Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of extends keyword in a companion object definition [duplicate]

I found this code example in Programming in Scala, 2nd Ed. (Chapter 25, Listing 25.11):

object PrefixMap extends {
  def empty[T] = ...

  def apply[T](kvs: (String, T)*): PrefixMap[T] = ...

  ...
}

Why is the extends clause there without a superclass name? It looks like extending an anonymous class, but for what purpose? The accompanying text doesn't explain or even mention this construct anywhere. The code actually compiles and apparently works perfectly with or without it.

OTOH I found the exact same code on several web pages, including this (which looks like the original version of the chapter in the book). I doubt that a typo could have passed below the radars of so many readers up to now... so am I missing something?

I tried to google it, but struggled even to find proper search terms for it. So could someone explain whether this construct has a name and/or practical use in Scala?

like image 946
Péter Török Avatar asked Nov 18 '22 15:11

Péter Török


1 Answers

Looks like a print error to me. It will work all the same, though, which probably helped hide it all this time.

Anyway, that object is extending a structural type, though it could also be an early initialization, if you had with XXX at the end. MMmmm. It looks more like an early initialization without any class or trait to be initialized later, actually... structure types do not contain code, I think.

like image 123
Daniel C. Sobral Avatar answered Dec 01 '22 00:12

Daniel C. Sobral