Scala has string interpolation like raw"\n"
for raw strings.
Does it have anything like number interpolation e.g. 1px
for one pixel? A nice syntax for numeric units would both make code more readable and make it easier to write safer code.
Like strings, numbers have a nice literal syntax and are fundamental.
prefix notation px(1)
is not how people write units:
case class px(n: Int)
And I don't think a postfix notation via implicit conversion can work:
case class Pixels(n: Int) {
def px() = Pixels(n)
def +(p: Pixels) = p match {case Pixels(m) => Pixels(n+m)}
}
implicit def Int2Pixels(n: Int) = Pixels(n)
it needs a dot or space or parens (i.e. not
(1 px)
or(1)px
or1.px
, which is not how humans write units).it won't check types i.e. we want to explicitly cast between these numeric type-alias things and numbers themselves (i.e.
1.px + 2
and1 + 2.px
anddef inc(p: Pixels) = p + Pixels(1)
withinc(0)
all don't fail, because of the implicit cast, when they should).