null this, classes, methods and "null this" assertion

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 10 19:17:31 PDT 2015


On Fri, 10 Apr 2015 22:06:53 -0400, Steven Schveighoffer wrote:

> On 4/10/15 9:59 PM, ketmar wrote:
>> let's take a code like this:
>>
>> import std.stdio;
>>
>> class A {
>>    size_t len = 42;
>>    final size_t length () const { return (this !is null ? len : 0); }
>> }
>>
>> void main () {
>>    A a;
>>    writeln(a.length);
>>    a = new A();
>>    writeln(a.length);
>> }
>>
>> in "-release" mode this code outputs "0" and "42", as expected (by me).
>> but without "-release" is raises "null this" assertion.
> 
> In non-release mode, it's calling the invariant, which is a virtual
> function.
> 
> -Steve

yet compiler can generate `CondExp` instead of `AssertExp`, and don't 
fail when `this` is `null`.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150411/6ad22848/attachment.sig>


More information about the Digitalmars-d mailing list