0

Can generics be used in Scala infix notations?

For example:

// Example from play-json

Json.obj("name" -> "Joe") \ "name" as[String] // Generics in infix notation
                                              // error: type application is not allowed for postfix operators

Ideally, I would like to achieve API like:

Json.obj("name" -> "Joe") \ "name" as String // But I think its implossible

Of course, using standard dot notation with parentheses it works fine:

(Json.obj("name" -> "Joe") \ "name").as[String]

The only worthy discussion that I found: https://groups.google.com/forum/#!msg/scalatest-users/Ujn2d2MdXm0/yvHpk1pOlDMJ

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96

1 Answers1

0

Infix notation can only be used for parenthesis not the square brackets which capture the type information. If you give the type hint on LHS, then it can be dropped.

Dragonborn
  • 1,755
  • 1
  • 16
  • 37