8 ms·
T* makes for a poor optional<T&>
- formerly_proven 5y agostd::optional makes for a poor optional simply because doing the right thing is not the default, requires more typing and uses an interface that doesn't exist in T*.
- assbuttbuttass 5y agogo suffers from this problem. Lack of generics makes it impossible to define an optional<T> type, and *T is annoying to use and requires an extra allocation. In practice, people use a zero value to indicate that a value is missing, but this only works if the zero value isn't also a valid input. Hopefully the coming generics will resolve this pain point.
- SQueeeeeL 5y agoOne philosophy is that zero input basically never be a valid input unless you're taking in integers. https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/ https://www.infoq.com/presentations/Null-References-The-Bill... . The idea is to try and never create null objects. I think it's interesting and helpful in practice to check/fail early.
- samhw 5y ago> The idea is to try and never create null objects This is one of those things which sounds great in practice, and survives approximately 17 seconds of working on a real codebase.
- SQueeeeeL 5y agoYeah, I think this might be a tab/spaces mixing situation like python had, where it had to be restricted by the language compiler because it's just so tempting.
- simiones 5y ago> One philosophy is that zero input basically never be a valid input unless you're taking in integers This is obviously not possible, since you can imagine lots of complex objects are all integers or themselves composed of integers, and for this entire class of objects, the zero value usually makes sense. For example, a 3-vector (in the physics sense) is a 3-tuple of integers `type Vec3 struct {x, y, z int}` , `Vec3 {0, 0, 0}` being the origin. How does a function specify that it can take an optional 3-vector? Even worse, you can imagine a struct for a potenitally 0-volume cube represented as 8 Vec3s, one for each vertex, whose 0 value is again a valid object. Of course, you could gratuitously add an `IsValid bool` flag which must be true to the class, but the cube example shows how this quickly becomes annoying and bloated.
- SQueeeeeL 5y agoIn this case, the memory pointer for Vec3 would not be null? It will be a defined memory address which points to 3 zeros. I think it's a difference of objects and primitive data types.
- simiones 5y agoThe discussion was that in Go, given the current lack of generics, you have two options to have an optional parameter: 1. Take a *Vec3, using nil as the "optional is missing" value. 2. Take a Vec3, but consider some value as "invalid", often the 0-value as possible. 1 invokes exactly the issues in Hoare's essay. 2 doesn't work for all types, as some types don't have any bit pattern that are not useful, this is what I was trying to point out.
- SQueeeeeL 5y agoI can't think of any situations where (1) would be insufficient, but I'm probably wrong. But I agree that setting an isValid bit for every object is overkill.
- saghm 5y agoI think the point is that it would be nice to be able to express the idea of a value not being present without having to risk accidentally trying to dereference a nil pointer. That's what Option brings to the table, although arguably Go could just provide an implementation of that like they do for slices and maps without actually having to introduce full generics to the language.
- Jtsummers 5y agoBut many types do have a "natural" zero value, that you may want to use, beyond just the numbers. Consider the empty string, empty list, empty dictionary, empty set. Those are zero values for their types. By reserving them as pseudo-nulls you lose the ability to distinguish between an object which happens to be empty and an object which is meant to mean "null". This is where Optional (or similar mechanisms) come in handy. If you actually want to avoiding creating null, Optional is a cleaner and more consistent way to accomplish this without needing to magic up a null-value in your type's valid set of values or create a one-off Optional (MaybeDictionary, MaybeList).
- saghm 5y agoIt's even worse than that in my opinion: the zero value for maps and slices is actually nil even though they're not pointers, meaning the empty values are not the zero values. This lets you do "fun" things like this (my memory of the syntax might be slightly off): ``` var map1 map[int]int = nil var map2 map[int]int = &map1 var map3 map[int]int = nil ``` Interface types also can be nil even if they're not explicitly pointers because heap pointers are needed under the hood due to the size of the value not being knowable at compile time. You can pretty quickly get into some hairy situations trying to ensure that a value isn't nil when combining pointers or maps/slices with interfaces. I really wish that separating the concerns of present versus not present from value versus reference was more mainstream. I understand why this was traditionally the case for lower-level languages, but even higher-level ones like Java are mostly based on the paradigm that only references can be "not present". This irks me in a similar way to how earlier versions of Java wouldn't allow default implementations of interface methods and instead requiring abstract classes, which classes could not extend from more than one of.
- wahern 5y ago> people use a zero value to indicate that a value is missing Go supports returning multiple results. Maybe I don't read enough Go code to understand what's actually popular, but AFAIU the idiomatic way to accomplish this in Go is something like: if v, exists := lookup(key); exists { do(v) } or if r, err != foo(); err != nil { do(r) } The compiler, unfortunately, can't enforce proper conditionals. OTOH, unlike C compilers Go won't make any dangerous assumptions about dereferencing nil pointers, which is something. I'm not sure Generics can help here. What you really want is compiler enforcement of comprehensive condition checks. For example, via pattern matching switch statements. That's easiest to do with optional types as the unwrapping operation creates a simple point for static constraint checking, but in principle the same thing could be accomplished with annotations on multi-value return types that describe the association.
- assbuttbuttass 5y agoI'm talking about optional inputs, you're right that multi-return works for optional outputs. How would you write a function that takes a string, but can use some default if none is provided? Common way: func f(s string) { if s == "" { s = "default" } ... }
- vishvananda 5y agoYou could always use a pointer for this, but admittedly it is pretty ugly when compared to a true optional type: func f(s *string) string { ret := "" if s == nil { ret = "default" } else { ret = *s } return ret } func main() { s := "foo" fmt.Println("Hello, " + f(&s)) fmt.Println("Hello, " + f(nil)) }
- kubb 5y agoGenerics will let you define an Option[any], just hope that your code reviewers don't decide that this isn't goey, and you should "keep it simple" or "just use a pointer" or something in that vein. There won't be overloading * for getting the value inside and overloading bool conversion to check if the value is there. Still, it's an improvement.
- adamc 5y agoWhat is "facially reasonable" supposed to mean?
- steveklabnik 5y agoMy understanding (not the author): "prima facie" is a legal term for "on its face", and this is saying the same thing but phrased in a different way. It looks reasonable on its face, due to being "superficially similar" (author's words). But when you look a bit deeper, it is not.
- jimminy 5y agoIt seems like an unusual rephrasing of the saying "reasonable on the face", which means "reasonable upon first appearance, without considering deeper aspects." It's judging a book by it's cover.
- deleted 5y ago[deleted]
- kentonv 5y agoNote the original title is: T* makes for a poor optional<T&> The last `<T&>` there is key, but seems to have been removed from the HN version of the title. This blog post is not arguing for the use of optionals in general (it assumes everyone already agrees with that). It is instead making a much more wonky argument that std::optional<T> should support T being a reference type, which apparently is not supported right now. For most purposes, it seems std::optional<T&> would behave very similarly T*, but the ergonomics would be better in certain template scenarios. It seems like a valid argument to me, but definitely getting into the weeds a bit. Aside: IMO std::optional in general is disappointing because it doesn't actually solve the worst problem with nullable pointers: dereferencing a null std::optional is still undefined behavior. I wish the standard had gone with a design that forces the developer to write an explicit check when unwrapping the optional...
- strager 5y ago> I wish the standard had gone with a design that forces the developer to write an explicit check when unwrapping the optional... If this is how std::optional worked, hardly anyone would use it.
- winter_squirrel 5y agoWhy, other languages that require unwrapping it still see plenty of people using optional. In fact, I would have more inclination to use it in my projects if that was the only way to do it.
- kentonv 5y ago> If this is how std::optional worked, hardly anyone would use it. OK... Well, this is how kj::Maybe works and having written several hundred thousand lines of code using it, I think it works well. https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes https://github.com/capnproto/capnproto/blob/master/kjdoc/tou...
- kllrnohj 5y agoWell, that seems to be using macros to cover up the syntax, which of course the C++ committee isn't going to recommend or use for anything new (nor should they). The actual implementation of Maybe doesn't seem to force anything, it just returns a pointer to the stored value. There's no enforcement of any kind that anything is checked as a result. There's some hoop-jumping with friend classes to "hide" the readMaybe() function to make the non-macro usage as ugly as possible to strongly encourage using the macro-convention, but that's hardly "enforcement". It's a cute syntax & macro pattern, but it's hardly a good fit for a C++ standard, either.
- lowbloodsugar 5y agoIf a developer is committed to using template-fu, then of course T* doesn't fit nicely with all the template magic, and the developer should use a template magic version of T* which is 'optional'. For those who don't like template-fu, T* is way better than 'optional', not simply because 'optional' is a template and therefore bad, but because T* works just fine in this world. So I am confused by the point of this article. It's like saying "gasoline makes a poor diesel when used in my diesel engined car."
- strager 5y agoI agree. Just because T* has problems in some contexts doesn't mean optional<T&> should be used in all context.
- MauranKilom 5y ago> Just because T* has problems in some contexts doesn't mean optional<T&> should be used in all context. Where does the article suggest using optional<T&> in all contexts?
- deleted 5y ago[deleted]
- isaacimagine 5y agoRust turns an Option<T> into a raw nullable pointer[1] (with NULL being Option::None). This is cool because a 2-item enum with a single pointer would usually be 8 (ptr) + 1 (tag) = 9 (total) ≈ 16 (padded) bytes, but it's only 8 because of this. I'm simplifying a tad, look up 'null pointer specialization rust' for more. What's cool is the zero-cost abstraction over type layout specification, while allowing the generic Option<T> type to work exactly as expected. [1]: Assuming T is a non-nullable pointer type.
- loeg 5y ago> Assuming T is a non-nullable pointer type. You can even do the same thing for a few special primitive integer types, such as std::num::NonZeroU64. https://doc.rust-lang.org/std/num/struct.NonZeroU64.html https://doc.rust-lang.org/std/num/struct.NonZeroU64.html : > Option<NonZeroU64> is the same size as u64 Behind the scenes, the type is defined with a magic compiler-internal incantation that isn't usable in user code, unfortunately: #[repr(transparent)] #[rustc_layout_scalar_valid_range_start(1)] #[rustc_nonnull_optimization_guaranteed] https://doc.rust-lang.org/src/core/num/nonzero.rs.html#39 https://doc.rust-lang.org/src/core/num/nonzero.rs.html#39
- tialaramex 5y agoYes, more of these would be nice, the ones we have are very valuable but it would be nice to make our own, ideally safely, but unsafe ones made carefully in a trustworthy library are good too. I would also like being able to tell the compiler about other niches. If a Foozle can be Ding, Ping, Zing or Number(n) where I promise n is from 0 to 199, that all fits in a byte if the compiler is willing to work with me by storing Ding, Ping and Zing as magic sentinels in the niche range 200 to 255 and I'm willing to trade the small runtime cost for the memory usage win.
- loeg 5y ago> If a Foozle can be Ding, Ping, Zing or Number(n) where I promise n is from 0 to 199, that all fits in a byte if the compiler is willing to work with me by storing Ding, Ping and Zing as magic sentinels in the niche range 200 to 255 and I'm willing to trade the small runtime cost for the memory usage win. Yeah, that would be cool. I think it would make sense to represent this as a repr(u8) enum with a range-limited Number variant. Something like: #[repr(u8)] enum Foozle { Ding, Ping, Zing, Number(u8 in 200..=255), // or whatever range syntax }
- psyclobe 5y agostd::optional<std::reference_wrapper<T>> is how we deal with this [lovely isn't it?].
- twic 5y agoIndeed. The author writes: > Whenever the idea of an optional reference comes up, inevitably somebody will bring up the point that we don’t need to support optional<T&> because we already have in the language a perfectly good optional reference: T*. But i have never heard this said even once. Rather, inevitably someone will bring up the point that you can do it with reference_wrapper. Check it out: https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper#Example https://en.cppreference.com/w/cpp/utility/functional/referen...
- kazinator 5y agoWell, I mean look, T* is a poor optional<T> simply because an optional<T> behaves like a T when present, whereas a T* makes no pretense that it's not a T. If X isn't Y, and doesn't even try to sort of be a Y when you need it to in useful circumstances, then X is a poor optional Y for that excellent reason. A tail is a poor optional leg, for starters, and so on. What you need is some_kinda_smart_pointer<T> instead of T*, which can be T* when you need it to, but also quacks like T most of the time; like when passed as an argument to a function needing a T, it will convert to that by yielding the T value and so forth. The name "some_kinda_smart_pointer" can be "optional", and QED. A T* isn't a class type; it's a basic type: a pointer. You can't do too many clever things with basic types in C++. They can't have any member functions, so they cannot substitute anywhere where something is required that has member functions. TLDR: naked pointers inherited from C have disadvantages compared to smart pointers in all sorts of generic programming and whatnot; ask any C++98 programmer.
- kllrnohj 5y agoI think you greatly misunderstood the article. There's no issue with optional<T>, nor is the article complaining that T* is a bad optional<T>. The problem is that you can't have optional<T&>. So if you want to pass an optional reference to something, you're currently forced to pass T* instead (using null as the proxy for "not passed", of course). And thus, T* makes for a poor optional<T&>.
- kazinator 5y ago> no issue with optional<T> Didn't say there is (or, for that matter, that there isn't). The author does say there is an issue with it: he would like optional<T&> but cannot have it. (That, of course, is not optional's fault, though). > nor is the article complaining that T* is a bad optional<T>. But it is poor (not just in comparison to a fictitious optional<T &>). > So if you want to pass an optional reference to something, you're currently forced to pass T* instead (using null as the proxy for "not passed", of course). And thus, T* makes for a poor optional<T&>. If you want an optional reference to something, maybe you don't entirely understand references. A reference isn't optional; it has to refer to something. References in C++ are odd; they have limitations. For instance, you can't declare an array of them, either. To simulate an optional reference, we use a pointer, and we can wrap that in a class: class optional_int_ref { const int *maybe_int; // null if we aren't referencing an int, otherwise valid pointer }; Then of course we want that to be a template to make it generic. std::optional is a container: it contains a value, which may be indicated as not being present. A reference isn't a value which is contained; it's a value which is somewhere else. If you contain a reference, the reference must be resolved to a valid object. Basically, you don't want optional<T &>, but a parallel optional_ref<T>: something that optionally references a value, and if so, that value is somewhere else. So, today, we could make optional_ref<T>. If we did so, then T * would be a poor optional_ref<T> for the same reasons that it's a poor optional_ref<T &> that we don't have. Therefore, T * is not the best optional we can have.
- imron 5y agooptional<T> makes for a poor Option<T>
- overgard 5y agoI don't really use optional in C++, but it strikes me that it would be pretty hard to reason about the lifetime of the T&. Pointers have the same problem, but when you see a pointer you're automatically going to start thinking about ownership, whereas references (especially if auto comes into play) are a lot more subtle... Also if you're going to optionally store a reference why not just use a unique_ptr or a shared_ptr so ownership is much more obvious and safer. (I realize it might not be zero cost, but optional<T&> just seems like an invitation to hard to find bugs)