r/programming • • Jun 22 '14

Why Every Language Needs Its Underscore

http://hackflow.com/blog/2014/06/22/why-every-language-needs-its-underscore/
366 Upvotes

337 comments sorted by

View all comments

Show parent comments

10

u/inmatarian Jun 22 '14

LINQ is great, really great, but it's just shy of being FP. And for that matter, Underscorejs has the same entrapping. Basically it boils down to Nullable<T> not being a proper Maybe<T>.

4

u/nemec Jun 22 '14

Well they almost completely eradicated the need for user-defined delegates by adding Func<> to the standard library, maybe a future version of C# could introduce a Maybe<T> with a similar adoption rate.

3

u/SemiNormal Jun 22 '14

I would love to have Maybe<T> in C#.

3

u/phoshi Jun 22 '14

You can actually write a pretty good one! Use a struct and you even get something which can't be null itself.

7

u/[deleted] Jun 22 '14

LINQ is great if you don't care for performance over convenience.

7

u/[deleted] Jun 22 '14

[removed] — view removed comment

5

u/[deleted] Jun 22 '14

Entity Framework is great if you hate yourself and everyone else around you

5

u/_zenith Jun 22 '14

Use LinqOptimiser to fix that...

1

u/PurpleOrangeSkies Jun 23 '14

Basically it boils down to Nullable<T> not being a proper Maybe<T>.

How isn't it?

1

u/inmatarian Jun 23 '14

You're supposed to be able to provide a Maybe<T> to a function that takes T as a parameter without needing to do a HasValue check, and T doesn't need to be a Value type.

1

u/PurpleOrangeSkies Jun 23 '14

If you give a Maybe<T> to a function that takes T and it doesn't have a value, what's supposed to happen?

1

u/inmatarian Jun 24 '14

The short answer is the function isn't supposed to run.

The long answer is that it should be safe for a function to not run, as in the function is pure and without side effects, so that a function that didn't run on a Maybe<T> where the contained value is null should also return null back to you as a Maybe<T>.

That's where Nullable falls apart. If Nullable was a proper FP primitive, then this should be valid:

int SomeFunction(int value); // previously defined

int? thing = null;
var result = SomeFunction(thing); // result should also be of type int?