Tadej Mali
Tadej Mali

Reputation: 1143

Declare a generic class in scala without square brackets

When reading this article I came to the following syntax:

implicit val slaveCanRead: Slave HasPrivilege Read = null

The author says:

Also, please not that Slave HasPrivilege Read is just another notation for HasPrivilege[Slave, Read]

Keeping the example in basic scala, the example could also be

val foo: Map[String, Long] = Map()
val bar: String Map Long = Map()

I was looking for some documentation/articles that would explain this syntax but could not find any. Can someone point to the language feature which allows this syntax?

Upvotes: 4

Views: 164

Answers (2)

michid
michid

Reputation: 10824

This is an infix type. Thus

val map: Map[String, Int] = ...

is actually equivalent to

val map: String Map Int = ...

This is especially useful for the Function type so you can write

val f: Int => Int = ...

instead of

val f: Function[Int, Int] = ...

Upvotes: 2

HTNW
HTNW

Reputation: 29193

It’s really just as simple as T1 TCon T2 = TCon[T1, T2]. It’s section 3.2.8 of the language specification.

InfixType ::= CompoundType {id [nl] CompoundType}

If the infix type ends with : it is right associative, and otherwise it is left associative, just like methods, and mixing fixities is an error without parentheses.

Upvotes: 5

Related Questions