Current sentiment on Nullable.get

Sebastiaan Koppe mail at skoppe.eu
Fri Dec 14 23:39:16 UTC 2018


On Thursday, 13 December 2018 at 13:14:13 UTC, Jonathan M Davis 
wrote:
>> import std.typecons;
>> class C {
>> }
>> void main() {
>>      Nullable!C c;
>>      assert(c.isNull);
>>      c = null;
>>      assert(!c.isNull);
>> }
>> ---
>
> I don't see anything wrong with that code. It's exactly how 
> it's supposed to work.

It is confusing, it doesn't follow the principle of least 
astonishment.

> Right now, you can write a templated function that operates on 
> a Nullable without caring one whit about what type it contains, 
> and it will behave the same regardless of the type. Meaning 
> that something like this
>
> auto foo(T)(T t)
> {
>     Nullable!T n = t;
>     ...
>     auto bar = t.get;
>     ...
> }

In languages where nullable/optional/maybe is more central, you 
are advised to avoid calling get directly, and you are expected 
to always map or foreach over it.

Essentially it is a range with 0 or 1 element.

> Nullable is often used simply to denote whether the variable
> has been given a value yet

Do you consider null a value? Because Nullable does.

---
auto p = Nullable!(int*)(someFunctionThatReturnsNull());
while (p.isNull) {
   // never happens
}
---

---
int* p = someFunctionThatReturnsNull();
while (p is null) {
   // does
}
---


More information about the Digitalmars-d mailing list