3 ms·
This felt a little bit like I walked into an ongoing conversation, the fact it's the second post to a blog which appears to have begun already in mid-flow doesn
by tialaramex 4y ago
This felt a little bit like I walked into an ongoing conversation, the fact it's the second post to a blog which appears to have begun already in mid-flow doesn't help with that, so, maybe I completely misunderstood what's going on.
> In a language like Rust, implicitly introducing Option is barely acceptable: Option is already a priveleged type
There's nothing special about Option, it's not a langitem for example, as AddAssign, Box and FnMut are. It's not a built-in type like f32 or [T; N], it's defined in core, but so are lots of things nobody would claim are "privileged".
It's a vocabulary type, but so is Ordering, so is ControlFlow, so is Duration, we need to agree what to call things, otherwise it's going to be a struggle to develop software.
- masklinn 4y ago> There's nothing special about Option, it's not a langitem for example While Option is not a lang item, Some and None are. Because they are critical to the desugaring of `for` loops for instance. Thus Option is, transitively, a lang item.
- tialaramex 4y agoGood point, I'd forgotten Some and None are indeed needed to de-sugar for. Do you know of anywhere else the actual language guts need them ?
- masklinn 4y agoI can't think of an other one, but I won't claim exclusive knowledge of the language's desugaring.
- jenadine 4y agoThe `?` operator works with Option. I know it is actually through the Try trait internally, but since it is only implemented for a few built-in type, it makes Option special.
- masklinn 4y agoMeh. That's just because it's an experimental / unstable trait. That Rev or Peekable implement TrustedLen does not make them special.
- ecton 4y agoAdditionally, the compiler optimizes certain Option<T> types to not require additional storage space.
- Measter 4y agoThat optimization isn't specific to Option, it works for any enum.
- Waterluvian 4y agoYeah, agreed. Option often appears in my typescript code in the form of possibly undefined variables. I unpack the Option by using a condition if I want to handle it or assert() if I just want the application to panic. I love how TypeScript forces this. You need to narrow the “undefined” type away before you can use the object in any way. Ruling out undefined and narrowing the type is the equivalent of checking for Some or None.
- Nullabillity 4y agoIt's not quite the same though. Since TypeScript uses union types, `T | undefined | undefined` is equivalent to `T | undefined` (and might even be `T`, depending on how that is defined!), while `Option<Option<T>>` is always a distinct type from `Option<T>`
- masklinn 4y agoWhich is why TypeScript has T | null | undefined <taps temple>
- Waterluvian 4y agoThere’s no one right answer but this is how I pick, in many cases, when to use null vs undefined.
- eyelidlessness 4y agoAgree (at least for non-personal projects, where I might be more inclined to use Option/Result types). Wherever possible, I use null as an explicit value when a reference is present but not currently assigned a value, and treat undefined as absence of a reference. I try not to even use the undefined keyword if possible (which is sometimes necessary in types, but I can’t remember the last time I used it in runtime code).
- Nullabillity 4y agoThat still doesn't let you represent `Option<Option<Option<T>>>`, requires you to remember the difference between null and undefined, and fails silently if you screw any of it up.