3

At the moment, I'm thinking of representing price as follows:

case class Price(amount: Double)

Which allows me to do

val price = Price(4.52)

Is there any mechanism which would allow me to create a Price object as follows?

val price = $4.52
deltanovember
  • 42,611
  • 64
  • 162
  • 244

1 Answers1

8

No because $4 is a valid identifier and . signifies a method.

However you could say

val $ = Price 

to set $ equal to the price companion object and parenthetize the Double:

scala> val p = $(4.52)
p: Price = Price(4.52)

edit: another thing you could do would be:

scala> implicit def toDollar(d: Double) = new {
     |   def $ = Price(d)
     | }
toDollar: (d: Double)java.lang.Object{def $: Price}

scala> val p = 4.52 $
p: Price = Price(4.52)
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180