Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 642
Tadej Mali Avatar asked Mar 08 '23 18:03

Tadej Mali


2 Answers

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.

like image 92
HTNW Avatar answered Mar 10 '23 10:03

HTNW


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] = ...
like image 24
michid Avatar answered Mar 10 '23 09:03

michid