Null references (oh no, not again!)
BCS
ao at pathlink.com
Fri Mar 6 15:39:01 PST 2009
Reply to Christopher,
> BCS wrote:
>
>> Hello Christopher,
>>
>>> BCS wrote:
>>>
>>>> Your right, but if you switch to a class and factory with no public
>>>> constructor, you can make it work. The problem of perf going down
>>>> the drain is avoidable if you can (in that mode) enforce compile
>>>> time checking of most cases and requiter calls to do run time
>>>> checks for the rest. If the template works right, than flipping
>>>> back to alias/typedef mode leaves the run time checks and leave the
>>>> unchecked code as correct while doing away with the perf problems.
>>>>
>>> If you use a class, you're begging the question. It's just that
>>> you'll have a null NotNull!(T) rather than a null T.
>>>
>>> Granted, you can use opAssign(T) instead, but you still need
>>> contracts.
>>>
>> That won't be an issue because it's a run time concern and I am
>> proposing that the class based version never even run. In fact it
>> could even not be runable. All it does is enforce usage patterns that
>> do work correctly with a different set of definitions.
>>
> I don't understand what you are saying. If you can't run a program
> that uses NotNull, who in their right mind would use it?
>
something like this:
version(Enforce)
{
NotNull!(T) MakeNotNull(T)(T) { assert(false); }
bool MakeNotNull(T)(T, out NotNull!(T)) { assert(false); }
class NotNull(T)
{
static this(){ assert(fasle);
// overload to make valid stuff work and invalid not work
}
}
else
{
template NotNull(T) // not quier right for classes but oh well
{
alias *T NotNull;
}
NotNull!(T) MakeNotNull(T)(T t)
{
if(t is null) throw new Something();
return t;
}
bool MakeNotNull(T)(T t, out NotNull!(T) tout)
{
if(t is null) return false;
tout = t;
return true;
}
}
compile it with -version=Enforce.
If it compiles, recompile without that version and run it
if not, fix it & try again
More information about the Digitalmars-d
mailing list