4 ms·
> Everyone who wanted to do basic things like read input or write output would have to understand how option type, in addition to discussion of what templated t
by alkonaut 9y ago
> Everyone who wanted to do basic things like read input or write output would have to understand how option type, in addition to discussion of what templated types are
I don't necessarily think that's a bad thing. Something that may be null is already an option type. It's "some T or null". Making the compiler force you into checking which it is, is no difference from checking for the presence of null. There is literally no difference in complexity - it's actually simpler because you don't have the cognitive overhead of remembering where a certain variable has and hasn't been null checked (After a null check, your variable has magically changed type to "some T, definitely not null".)
x = getsomething
if x != nil
dosomething(x)
and
x = getsomething
if x.hasvalue
dosomething(x.value)
The programmer alone has to remember this - remember to check, and the status of each variable that can be null. Was x checked before? scroll up to see...
Add 10 variables and some nested ifs and ask the programmer which variables can and can't be null at any given place. This is the job of a compiler, not a programmer. And if my experience is any guide, the less experienced programmers, which are the ones we are discussing now, would be happy to have 100 locals or 12 levels of nested ifs in a method. They need this added help more than the people who ask for it.
- catnaroek 9y agoNo, nulls aren't options. Options nest. You can have Some(Some(Some(foo))). The non-compositionality of null makes it impossible to abstract it away.
- brandonbloom 9y agoHuh? What kind of "nesting" are you talking about? How is that different than nesting structs containing nullable pointers?
- catnaroek 9y agoIn ML, if you have a type “foo”, then you can also use the type “foo option option option” without creating any new nominal datatypes. In particular, using a value of type “foo option option option” is just as easy as using a value of type “foo” or “foo option”: case wrapped_foo of NONE => ... | SOME NONE => ... | SOME (SOME NONE) => ... | SOME (SOME (SOME foo)) => ... On the other hand, with your proposed alternative, you have to create a wrapper struct for every nullable kind of thingy: type MaybeFoo struct { foo *Foo } type MaybeMaybeFoo struct { foo *MaybeFoo } type MaybeMaybeMaybeFoo struct { foo *MaybeMaybeFoo } And, as if that weren't offensive enough, you have to manually pack those pointers into structs and then unpack them back.
- alkonaut 9y agoThe only type I'd make (if it wasn't in the standard library) is the regular Option<T>. If I have an instance of Option<T> and the method takes a T, then I'd have to check and unwrap it. I can't see the need for creating multiple structs assuming you can nest the Option<Option<T>> when required (and heap allocation is OK)? Perhaps I misunderstood something. If I have an instance of Option<Option<T>> then I'd have to unwrap it more than once. However, if the language has no concept of such automatic unwrapping, the chance that you would ever end up with a nested option is pretty slim. Basically, the monadic type use of these is an effect of the language doing that, not the other way. In e.g. C# where I use option types extensively I'm yet to see a wrapped one. In F# it's different. In C# there is a feature in which nullables (e.g. int? and int?? or int???) actually work similarly because there are "lifted operators" which sort of half-achieve this, e.g. int? x = 10; int? y = 20; int? z = x + y; but to be honest this to me feels mostly quirky because it doesn't extend to much else in the language.
- catnaroek 9y ago> Basically, the monadic type use of these is an effect of the language doing that, not the other way. In e.g. C# where I use option types extensively I'm yet to see a wrapped one. And C# pays dearly for it. Why doesn't C# have anything like C++'s <algorithm>, or Rust's composable iterators, or Haskell's plethora of composable abstractions (foldables, traversables, lenses, etc.)? > In C# there is a feature in which (...) but to be honest this to me feels mostly quirky because it doesn't extend to much else in the language. Agreed. Hardcoded special cases are always a bad idea. --- Sorry, HN won't allow me to make a new post because “I'm submitting too fast”, so here goes my reply: C++ has a finer-grained hierarchy of iterator concepts: (0) InputIterators correspond to C# IEnumerators. (1) OutputIterators allow you to write to the sequence's current element. (2) ForwardIterators allow you to dereference the current element arbitrarily many times before moving on to the next. (3) BidirectionalIterators allow you to move both forward and backwards in the sequence of elements. (4) RandomAccessIterators allow you to skip arbitrarily many positions in the sequence of elements in constant time. And there are algorithms defined in terms of these refined iterator concepts.
- alkonaut 9y agoYes I only meant in the sense of null adding a "maybe". x = fetch_an_int // x is definitely an int y = fetch_an_obj // y is *maybe* a T.
- deleted 9y ago[deleted]
- hota_mazi 9y agoNote that these is absolutely zero gains with the second approach if the compiler doesn't enforce it. And I very much don't envision that the Go compiler would be modified to make sure that `x.value` is never referenced unless the presence of that value has been confirmed by an earlier statement.
- alkonaut 9y ago> Note that these is absolutely zero gains with the second approach if the compiler doesn't enforce it. The compiler enforces it by the fact that you can't pass in the return value from one thing (that might not be a valid value) into the next function that takes an argument that must be a valid value. Your objection that "but I can unwrap/call value without checking so it's useless" is a commonly raised objection - but it's a false one. With nulls var x = get_something do_something(x) // ok even if x is null With option/maybe x = get_something do_something(x) // compiler err x = get_something do_something(x.value) // ok, *concious action by developer* x = get_something if x.hasvalue do_something(x) // ok The difference in safety between having to make a concius unwrap IS the whole point of the feature. This works exactly the same everywhere. You can always ".unwrap()" in Rust for example. Having to unwrap, or unwrapping without checking first isn't wrong, in fact it's often right. Unwrapping the value first means "extract the value if it exists, or otherwise panic right here". This decision to "assume success or otherwise panic" is a clear intent that the next developer can read.