www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - null this

reply Satoshi <satoshi gshost.eu> writes:
Hello,
how can calling method on local struct throw "null this"?
struct is initialized by .init and have default values.


struct Foo {
Bar bar;

     static struct Bar {
         float x, y, z;
     }

Bar getBar() {
return bar;
}

}


then
Foo foo = Foo.init;
auto b = foo.getBar(); // throw "null this"
Nov 21 2016
next sibling parent Satoshi <satoshi gshost.eu> writes:
cast(ulong)&foo but is not null.

It won't work just in special cases what I cannot reproduce 
easily.
Nov 21 2016
prev sibling next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 21/11/2016 11:25 PM, Satoshi wrote:
 Hello,
 how can calling method on local struct throw "null this"?
 struct is initialized by .init and have default values.


 struct Foo {
 Bar bar;

     static struct Bar {
         float x, y, z;
     }

 Bar getBar() {
 return bar;
 }

 }


 then
 Foo foo = Foo.init;
 auto b = foo.getBar(); // throw "null this"
Well that code definitely works fine as is. Perhaps remove that static, it makes me a little queasy just looking at it. Also no need to set foo to Foo.init, it already is that value.
Nov 21 2016
prev sibling parent Satoshi <satoshi gshost.eu> writes:
On Monday, 21 November 2016 at 10:25:17 UTC, Satoshi wrote:
 Hello,
 how can calling method on local struct throw "null this"?
 struct is initialized by .init and have default values.


 struct Foo {
 Bar bar;

     static struct Bar {
         float x, y, z;
     }

 Bar getBar() {
 return bar;
 }

 }


 then
 Foo foo = Foo.init;
 auto b = foo.getBar(); // throw "null this"
I had auto ref getBar() inout { return bar; } in which was problem. When I separated it into 2 funcs get/set it works fine.
Nov 21 2016