Click here to Skip to main content
15,905,782 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralHow about new syntactical sugar for exception checking? Pin
Chris Maunder21-Jul-16 6:58
cofounderChris Maunder21-Jul-16 6:58 
GeneralRe: How about new syntactical sugar for exception checking? PinPopular
Richard Deeming21-Jul-16 7:13
mveRichard Deeming21-Jul-16 7:13 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Ryan Peden21-Jul-16 9:08
professionalRyan Peden21-Jul-16 9:08 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Richard Deeming21-Jul-16 9:20
mveRichard Deeming21-Jul-16 9:20 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Ryan Peden21-Jul-16 9:28
professionalRyan Peden21-Jul-16 9:28 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Richard Deeming21-Jul-16 9:51
mveRichard Deeming21-Jul-16 9:51 
GeneralRe: How about new syntactical sugar for exception checking? Pin
BillWoodruff21-Jul-16 13:54
professionalBillWoodruff21-Jul-16 13:54 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Richard Deeming22-Jul-16 2:24
mveRichard Deeming22-Jul-16 2:24 
How about something like this:
C#
public abstract class DodgyResult<T>
{
    public abstract bool Succeeded { get; }
    public abstract T Value { get; }
    public abstract Exception Error { get; }
    
    public T GetValueOrDefault(T defaultValue = default(T))
    {
        return Succeeded ? Value : defaultValue;
    }
    
    public static DodgyResult<T> Success(T value)
    {
        return new SuccessResult(value);
    }
    
    public static DodgyResult<T> Failure(Exception error)
    {
        return new ErrorResult(error);
    }
    
    // Explicit cast to the return type; 
    // throws an InvalidOperationException if this is a failure result:
    public static explicit operator T(DodgyResult<T> result)
    {
        return result.Value;
    }
    
    // Allow the result to be treated as a bool value indicating success:
    public static bool operator true(DodgyResult<T> result)
    {
        return result.Succeeded;
    }
    
    public static bool operator false(DodgyResult<T> result)
    {
        return !result.Succeeded;
    }
    
    private sealed class SuccessResult : DodgyResult<T>
    {
        public SuccessResult(T value)
        {
            Value = value;
        }
        
        public override bool Succeeded => true;
        public override T Value { get; }
        public override Exception Error => null;
    }
    
    private sealed class ErrorResult : DodgyResult<T>
    {
        public ErrorResult(Exception error)
        {
            Debug.Assert(error != null);
            Error = error;
        }
        
        public override bool Succeeded => false;
        public override Exception Error { get; }
        
        public override T Value 
        { 
            // Wrap the error in a new exception to preserve the original stack trace:
            get { throw new InvalidOperationException(Error.Message, Error); }
        }
    }
}

public static class DodgyResult
{
    // Helper to let the compiler infer the generic parameter:
    public static DodgyResult<T> Success<T>(T value)
    {
        return DodgyResult<T>.Success(value);
    }
    
    public static DodgyResult<T> RunDodgy<T>(Func<T> dodgyFunc)
    {
        try
        {
            return Success(dodgyFunc());
        }
        catch (Exception ex)
        {
            return DodgyResult<T>.Failure(ex);
        }
    }
    
    public static DodgyResult<TResult> RunDodgy<T, TResult>(Func<T, TResult> dodgyFunc, T arg)
    {
        try
        {
            return Success(dodgyFunc(arg));
        }
        catch (Exception ex)
        {
            return DodgyResult<TResult>.Failure(ex);
        }
    }

    // TODO: "RunDodgy" overloads for the other Func<...> types...
}

Testing:
C#
var dr1 = DodgyResult.RunDodgy(SomeFuncOkay, "hello");
var dr2 = DodgyResult.RunDodgy(SomeFuncNull, "doctor");
var dr3 = DodgyResult.RunDodgy(SomeFuncError, "name");

SomeOtherMethod(
    (string)dr1, // Throws an exception if dr1 is a failure;
    dr2 ? (string)dr2 : null, // Using the bool operators and explicit cast;
    dr3.GetValueOrDefault("continue") // Providing a default value;
);

Now you don't need ref parameters, and you don't need to declare the variable on a separate line. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: How about new syntactical sugar for exception checking? Pin
BillWoodruff24-Jul-16 14:58
professionalBillWoodruff24-Jul-16 14:58 
GeneralRe: How about new syntactical sugar for exception checking? Pin
#realJSOP21-Jul-16 7:19
professional#realJSOP21-Jul-16 7:19 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Corporal Agarn21-Jul-16 7:27
professionalCorporal Agarn21-Jul-16 7:27 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Sander Rossel21-Jul-16 20:42
professionalSander Rossel21-Jul-16 20:42 
GeneralRe: How about new syntactical sugar for exception checking? Pin
#realJSOP21-Jul-16 23:36
professional#realJSOP21-Jul-16 23:36 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Eddy Vluggen21-Jul-16 7:46
professionalEddy Vluggen21-Jul-16 7:46 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Chris Maunder21-Jul-16 8:09
cofounderChris Maunder21-Jul-16 8:09 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Gary Wheeler22-Jul-16 1:05
Gary Wheeler22-Jul-16 1:05 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Vark11121-Jul-16 7:47
Vark11121-Jul-16 7:47 
GeneralRe: How about new syntactical sugar for exception checking? Pin
OriginalGriff21-Jul-16 8:02
mveOriginalGriff21-Jul-16 8:02 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Nish Nishant21-Jul-16 8:13
sitebuilderNish Nishant21-Jul-16 8:13 
GeneralRe: How about new syntactical sugar for exception checking? Pin
CPallini21-Jul-16 21:18
mveCPallini21-Jul-16 21:18 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Harley L. Pebley22-Jul-16 8:52
Harley L. Pebley22-Jul-16 8:52 
GeneralRe: How about new syntactical sugar for exception checking? Pin
BillWoodruff24-Jul-16 15:16
professionalBillWoodruff24-Jul-16 15:16 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Kornfeld Eliyahu Peter21-Jul-16 8:29
professionalKornfeld Eliyahu Peter21-Jul-16 8:29 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Brisingr Aerowing21-Jul-16 9:59
professionalBrisingr Aerowing21-Jul-16 9:59 
GeneralRe: How about new syntactical sugar for exception checking? Pin
Jörgen Andersson21-Jul-16 10:09
professionalJörgen Andersson21-Jul-16 10:09 

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

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