Extending Scala's arrow notation for pairs -
scala, in predef
, defines support ->
, →
(here cleaned bit):
final class arrowassoc[a](val __leftofarrow: a) extends anyval { def -> [b](y: b): tuple2[a, b] = tuple2(__leftofarrow, y) def →[b](y: b): tuple2[a, b] = ->(y) }
this conveniently allows create pairs using arrow notation instead of plain tuple syntax:
scala> "foo" → 42 res0: (string, int) = (foo,42)
this arrow syntax can extended type definition , extractor (here →
shown):
type →[a, b] = (a, b) object → { def unapply[a, b](t: (a, b)) = some(t) }
this allows write things like:
"a" → 42 match { case → b ⇒ println(s"found `$a` , `$b`") }
and:
def foo[a, b](t: → b) = ???
i wondering if there reason scala doesn't define in standard library. there downsides these definitions?
here 1 downside. being able both
def foo[a, b](t: -> b) = ???
and
def foo[a, b](t: => b) = ???
would confusing. if types don't line up, compiler catch of course. however, experienced scala programmer understand error message quickly.
Comments
Post a Comment