Welcome to the Lounge
The Lounge is rated PG. If you're about to post something you wouldn't want your
kid sister to read then don't post it. No flame wars, no abusive conduct, no programming
questions and please don't post ads.
|I am testing several depths of properties in an object to make sure they are safe before I call them:
if (myObject != null)
if (myObject.MyProperty != null)
I was thinking it would be really cool to be able to do
but obviously if
myObject == null then we have a runtime null ref error because, regardless of what the NotNull method returns as part of the chaining,
myObject is still null.
So this got me thinking: You can do
if (myObject != null && myObject.MyProperty != null)
because of short circuit boolean evaluation in C#, but I was wondering, with my fairly mainstream experience in languages, if there are languages out there that would allow chaining of methods with short circuit evaluation.
Essentially you'd have to have the input parameter be resolved after the method was called in order to have the method be able to say "I don't need the input parameter, please just ignore it".
Has anyone heard of this? Would it open up a World Of Pain when it comes to debugging? Would it be useful? Am I procrastinating?
Update: and it turns out this leads into a great discussion of extension methods. See
The Maybe Monad[^] and Chained null checks and the Maybe monad[^] for two ways of achieving this. Once you've done that, debate the correctness of extension methods that are able by design to operate on a null references.
I will be over there looking for new, shiny, distracting things.
The Code Project | Co-founder
Microsoft C++ MVP
modified 10-Jan-13 16:48pm.
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.