Scala: Option[T] as ?[T] (or even T?) -
i tried
type ?[_] = option[_] def f(x: ?[int]) = (y <- x) yield y
(but don't know doing.)
insofar types objects, should able define postix operator (i.e. 0 arity method) use in type signatures (i think). might need space like
def f(x: int ?) = (y <- x) yield y
scala makes easy use option type matching , polymorphism, avoid null. but, classes (nullable) vars , java returns vars. using classes , calling java 2 of scala's selling points. easy-to-write , easy-to-read syntax support options more strongly.
what things scala "?" make parsing special.
ideally, 1 write
def f(x: int?) = (y <- x) yield y
like other languages. can in scala (without macro)?
first, types not objects. in fact, scala has 2 namespaces: values , types. different things, , play different rules.
the postfix idea kind of nice, actually, not possible. there's infix notation types, though.
now, wrote:
type ?[_] = option[_]
each underscore has different meaning. underscore in ?[_]
means ?
higher-kinded, don't care it's type parameter is. underscore in option[_]
means option
existential type. when write x: ?[int]
, scala convert x: option[t] { forsome type t }
. means not don't int
, type parameter of option
unknowable (you know exists).
however, does compile:
scala> def f(x: ?[int]) = (y <- x) yield y f: (x: ?[int])option[any]
which version of scala did use? 2.11? co-worker of mine has found type inference regressions on 2.11, it.
the proper way write type alias this:
type ?[+a] = option[a]
not pass type parameter along (it is parameter, after all!), need specify co-variance act option
(which co-variant itself).
now, 2 questions, scala has absolutely no special treatment of ?
. and, no, can't this. ?
not widespread among languages either, , in of them support it, built in language, , not externally defined.
besides, it's kind of joke that, when interface java, typing out option
problem -- not average identifier size in java!
Comments
Post a Comment