top | item 13835273

(no title)

canadaj | 9 years ago

I see your point on out parameters, they are ripe for abuse. You can abuse out parameters right now, just with a little more effort.

    var x, y;
    p.DoSomething(out x, out y);
Where this becomes useful, I think, are places where you can't avoid using an out parameter. Now, you can choose to throw away or use the out parameter without explicitly declaring it.

    var isNumber = Int32.TryParse(number, out x);

discuss

order

louthy|9 years ago

Option types are the way to go for something like this:

    public static class Parse
    {
        public static Option<int> TryInt(string str)
        {
            int value;
            return Int32.TryParse(str, out value)
                ? Some(value)
                : None;
        }
    }

    var x = Parse.Int("123").IfNone(0);

    var x = Parse.Int("456").Match(
                Some: x  => x * 2,
                None: () => 0
            );

    Parse.Int("456").Iter(Console.WriteLine);
https://github.com/louthy/language-ext/blob/e6ff382a1752c7de...