Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy except programming questions.

Technical discussions are encouraged, but click here to ask your programming question.

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.


 
GeneralRe: Job Offer! PinmemberNagy Vilmos10-Jan-13 6:40 
GeneralRe: Job Offer! PinmemberColin Mullikin10-Jan-13 6:45 
GeneralRe: Job Offer! PinmvpCPallini10-Jan-13 6:52 
GeneralRe: Job Offer! Pinmemberjeron110-Jan-13 7:23 
GeneralRe: Job Offer! PinmemberMathlab10-Jan-13 7:21 
GeneralRe: Job Offer! PinmemberPHS24110-Jan-13 7:22 
GeneralRe: Job Offer! PinmentorDaveAuld10-Jan-13 7:45 
GeneralRe: Job Offer! PinmemberWjousts10-Jan-13 8:05 
GeneralRe: Job Offer! PinmemberVasudevan Deepak Kumar10-Jan-13 8:11 
GeneralRe: Job Offer! PinmemberDeyan Georgiev10-Jan-13 9:07 
GeneralRe: Job Offer! PinmemberJörgen Andersson10-Jan-13 9:26 
GeneralRe: Job Offer! PinmemberMike Hankey10-Jan-13 10:14 
GeneralRe: Job Offer! Pinmemberloctrice10-Jan-13 10:42 
GeneralMethod chaining with short-circuit parameter evaluation [modified] PinadminChris Maunder10-Jan-13 4:53 
I am testing several depths of properties in an object to make sure they are safe before I call them:
Test.NotNull(myObject);
if (myObject != null)
{
    Test.NotNull(myObject.MyProperty);
    if (myObject.MyProperty != null)
        Test.IsPositive(myObject.MyProperty.Id);
        ...
}
I was thinking it would be really cool to be able to do
Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id);
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.
cheers,
Chris Maunder
 
The Code Project | Co-founder
Microsoft C++ MVP


modified 10-Jan-13 16:48pm.

GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberDalek Dave10-Jan-13 5:00 
GeneralRe: Method chaining with short-circuit parameter evaluation PinadminChris Maunder10-Jan-13 5:07 
GeneralRe: Method chaining with short-circuit parameter evaluation PingroupNorm .net10-Jan-13 5:14 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmember Shelby Robertson 10-Jan-13 5:41 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberPaul Conrad10-Jan-13 7:06 
GeneralRe: Method chaining with short-circuit parameter evaluation PingroupNorm .net10-Jan-13 21:20 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberDalek Dave10-Jan-13 5:18 
JokeRe: Method chaining with short-circuit parameter evaluation Pinmemberdjj5510-Jan-13 5:34 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberPIEBALDconsult10-Jan-13 5:01 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberMaximilien10-Jan-13 5:03 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmemberpeterchen10-Jan-13 5:06 
GeneralRe: Method chaining with short-circuit parameter evaluation PinadminChris Maunder10-Jan-13 5:08 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmemberpeterchen10-Jan-13 5:20 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberDalek Dave10-Jan-13 5:26 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberNagy Vilmos10-Jan-13 5:34 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberDalek Dave10-Jan-13 5:37 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberNagy Vilmos10-Jan-13 5:40 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmemberjibesh10-Jan-13 6:03 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmembergavindon10-Jan-13 5:08 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmvpEspen Harlinn10-Jan-13 5:11 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmvpEddy Vluggen10-Jan-13 5:53 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberJörgen Andersson10-Jan-13 9:42 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberRanjan.D10-Jan-13 6:01 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmvpDave Kreskowiak10-Jan-13 5:13 
GeneralThere is a cool pattern PinmemberEnnis Ray Lynch, Jr.10-Jan-13 5:22 
GeneralRe: Method chaining with short-circuit parameter evaluation PinprotectorPete O'Hanlon10-Jan-13 5:43 
GeneralRe: Method chaining with short-circuit parameter evaluation PinadminChris Maunder10-Jan-13 11:53 
GeneralRe: Method chaining with short-circuit parameter evaluation PinprotectorPete O'Hanlon10-Jan-13 11:58 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmembermark merrens10-Jan-13 5:54 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberRoger Wright10-Jan-13 6:02 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberNemanja Trifunovic10-Jan-13 6:11 
GeneralRe: Method chaining with short-circuit parameter evaluation PinadminChris Maunder10-Jan-13 11:54 
GeneralRe: Method chaining with short-circuit parameter evaluation PinprotectorAspDotNetDev10-Jan-13 6:15 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmemberszukuro10-Jan-13 6:21 
GeneralRe: Method chaining with short-circuit parameter evaluation Pinmemberszukuro10-Jan-13 6:18 
GeneralRe: Method chaining with short-circuit parameter evaluation PinmemberZac Greve10-Jan-13 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 29 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid