DB ORM

Timon Gehr timon.gehr at gmx.ch
Thu Aug 11 06:32:20 PDT 2011


Graham Fawcett wrote.
> On Thu, 11 Aug 2011 20:10:15 +0800, zhang wrote:
>
>>> > I think D needs user defined attributes first.
>>
>> About attribute, here is an example:
>>
>> ....
>>
>> There is a problem that is D's basic type is not nullable. In C#,
>> the nullable integer type can be defined as "Int?" or
>> "Nullable<int>".
>
> You don't need attributes for that: you can just define a "struct
> Nullable(T)" that wraps the value, and provides a way to express a
> null value.
>
>     struct Person {
>       int ID;           // required
>       Nullable!int age; // optional
>       ...
>     }
>
>     void foo(Person p) {
>       if (p.age.isNull) ...
>       else writeln(p.age + 100);
>     }
>
> Graham

Alternatively you just use a class to wrap the value:

template Nullable(T){
    static if(is(T == class)) alias T Nullable;
    else class Nullable{T v; alias v this;}
}


The benefit of this approach is that you don't have to invent new ways to test for
null values.


More information about the Digitalmars-d mailing list