Nullable types

Bill Baxter wbaxter at gmail.com
Mon Oct 20 18:50:32 PDT 2008


On Tue, Oct 21, 2008 at 10:33 AM, Lionello Lunesu
<lionello at lunesu.remove.com> wrote:
>
> "Bill Baxter" <wbaxter at gmail.com> wrote in message
> news:mailman.176.1224549806.3087.digitalmars-d at puremagic.com...
>>
>> On Tue, Oct 21, 2008 at 9:35 AM, Bill Baxter <wbaxter at gmail.com> 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/
>>
>> Also C++ has *non*-Nullable types in the form of references.
>>
>> void aFunction(ref Struct xyz) {
>>    // &xyz is a pointer that can't be null
>> }
>>
>> ref Struct anotherFunction() {
>>   ...
>> }
>>
>> &(anotherFunction()) --> can't be null
>>
>> I think that may be a C++ FAQ  for "When do I use references vs
>> pointers?"  One answer to that is that if you don't want to allow
>> NULLs, use a reference.  If you do, then use a pointer.
>
> But C++ references behave very differently from non-null-references. For
> one, you can't change what is being referenced.

Yep, that's different.  As I understand it C++ disables rebinding just
because rebinding would require introducing a new "reference
assignment" operator to distinguish from plain a=b which operates on
the thing referred to not the reference itself.  And Bjarne thought
that would introduce too much complexity for too little benefit.  Most
of the time if you need to rebind a reference, you can just create a
new local variable for it.  My hunch is that covers about 90% of use
cases.

Anyway, maybe Bartosz will solve all our problems when part 2 of his
blog post comes out:
http://bartoszmilewski.wordpress.com/2008/10/18/who-ordered-rvalue-references-part-1/
;-)

--bb



More information about the Digitalmars-d mailing list