Struct with default ctor (Was: [dmd-beta] dmd 2.064 beta take 2)

Byron Heads byron.heads at gmail.com
Mon May 20 09:05:18 PDT 2013


On Mon, 20 May 2013 11:43:35 -0400, Andrei Alexandrescu wrote:

> On 5/20/13 11:19 AM, Byron Heads wrote:
>> On Sun, 19 May 2013 17:36:17 -0400, Andrei Alexandrescu wrote:
>>> OK, this is sensible. One question - would you be willing to type
>>> symbols as NullType!T instead of T to avoid these issues?
>>>
>>> Thanks,
>>>
>>> Andrei
>>
>> More boiler plate code for functions that take pointers.
>>
>> void foo(T)(T t)
>> 	if(isPointer!T)
>> {
>>      static if(isNullable!T)
>> 	if(!t) throw ....
>> }
> 
> But this goes both ways. Regardless of the default, you'd sometimes need
> to distinguish between cases. You either hurt one half of your cases or
> the other half. In fact you are now showing the case that assumes
> non-nullable being the default.
> 
>> May also introduce then need to check for objects in the init state
>> (default state)
>>
>>
>>
>> outside of NonNull in stdlib, an attribute maybe a possible answer.
>>
>> void bar(@nonnull SomeClass o)
>> {
>> 	o = foo(); // if foo returns @nonnull, then check is not needed,
>> else needs a check added during assignment }
>>
>> there are a few compile time checks that can be done to prove o is not
>> null, if not the compiler adds runtime checks.
> 
> I think a parameterized type is a better match for non-null because it
> attaches to the type, not the symbol.
> 
>> But really, checking for valid input is part of programming, might be
>> better to have lint integration that can help find these types of
>> problems
> 
> Agreed.
> 
> 
> Andrei



What about dealing with externs you want to protect?

extern(C) void foo(@nonnull int* x);

other then that I think the library solution is fine

@nonnull Bar* b <=> NotNull!(NotNull!Bar) b







More information about the Digitalmars-d mailing list