Nullable types
Bent Rasmussen
IncredibleShrinkingSphere at Gmail.com
Tue Oct 21 16:42:04 PDT 2008
Precisely. The solution they chose was adequate for the need at hand.
- Bent
"Denis Koroskin" <2korden at gmail.com> skrev i meddelelsen
news:op.ujds70e7o7cclz at proton.creatstudio.intranet...
> On Tue, 21 Oct 2008 05:02:48 +0400, Bill Baxter <wbaxter at gmail.com> wrote:
>
>> On Tue, Oct 21, 2008 at 9:40 AM, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>> Bill Baxter wrote:
>>>>
>>>> On Tue, Oct 21, 2008 at 9:11 AM, Lionello Lunesu
>>>> <lio at lunesu.remove.com>
>>>> wrote:
>>>>>
>>>>> Bent Rasmussen wrote:
>>>>>>
>>>>>> Not true. It wraps the value type in a struct with a boolean field
>>>>>> expressing whether it is null or not.
>>>>>>
>>>>>> http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx
>>>>>
>>>>> Indeed. Thanks for pointing that out.
>>>>>
>>>>> So C#'s 'foo?' syntax has even less to do with this compile-time
>>>>> nullness
>>>>> checking.
>>>>
>>>> Now it makes sense. Yes the C# feature is apparently just a
>>>> convenient way to create a value type with a special
>>>> "none-of-the-above" value.
>>>> I guess this feature is driven by need to connect with databases that
>>>> often have nullable types.
>>>>
>>>> This chapter of a C# 2.0 book covering Nullable Types seems to agree
>>>> with that assessment :
>>>> http://www.springerlink.com/content/w2mh0571776t3114/
>>>
>>> Not a very clever design. If they took a hit of a Boolean field (= a
>>> word
>>> field when padding is accounted for), they might as well have added a
>>> reference to Exception. That way, you not only know the value is null,
>>> you
>>> may also have info on why.
>>
>> Hmm, I was thinking storing a pointer to the value itself would make
>> more sense than a bool.
>> Along these lines:
>>
>> struct Nullable(T)
>> {
>> private T _value;
>> private T* _ptr; // maybe points to _value, maybe null
>>
>> T* ptr { return _ptr; }
>> T value() { return *_ptr; }
>> void value(T v) { _value = v; _ptr = &_value; }
>> void value(T* v) { if (v is null) { _ptr = null; } else { _value =
>> *v; _ptr = &_value; } }
>> }
>>
>> That way you just get a null pointer exception for free when you try
>> to use the nulled value.
>> But maybe that's not a workable approach in C#.
>>
>> --bb
>
> That's an overkill, operation cost gets too high for absolutely no reason.
More information about the Digitalmars-d
mailing list