Safe Navigation Operator “?.” for D2 ?
Bienlein
jeti789 at web.de
Mon Mar 3 03:46:27 PST 2014
On Thursday, 27 February 2014 at 13:27:14 UTC, Remo wrote:
>
> Apparently C# will get it in the next version.
> http://blogs.msdn.com/b/jerrynixon/archive/2014/02/26/at-last-c-is-getting-sometimes-called-the-safe-navigation-operator.aspx
>
> What do you think how well would this work in D2 ?
AFAIKS nullability in Kotlin has already been mentioned, but not
how you have to declare some value to be nullable:
var value : String = ""
value = "hello" // fine
value = null // compiler error: must not assign null
var valueOrNull : String?
valueOrNull = null // line 5
Assigning null to valueOrNull in line 5 is fine as valueOrNull is
declared to be String or null (denoted by the ? in String?).
However, also Kotlin does not get around the Scala/Rust-style
Otpion class this way, see
http://kenbarclay.blogspot.de/2014/02/kotlin-option-type-1.html
and
http://kenbarclay.blogspot.de/2014/02/kotlin-option-type-2.html.
Let's say you retrieve the value associated with some key from
some map and the key does not exist in the map. Then you would
get null from the retrieval and the compiler can't tell at
compile time. So you need return an Option object to force the
user to check for the value returned being null.
My preliminary conclusion to this is that in the end you have to
introduce an Option type as in other languages. I would say this
pretty much makes a need for a language extension obsolete. To be
consistent you would have to add new methods to Phobos and other
libraries to return Option (e.g., for the map class and others).
If you want to you can still add your ?. method to subclasses of
Option (aka None and Some).
-- Bienlein
More information about the Digitalmars-d
mailing list