www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct opCall makes a nan

reply "dominik" <asd asd.com> writes:
I'm confused by this:

struct vector3 {

  union {
   struct { float x, y, z; };
   struct { float r, g, b; };
   struct { float cell[3]; };
  };

}

struct plane {

  union {
   struct { vector3 N; float D; };
   struct { float cell[4]; };
  };

  plane opCall() {
   plane pl;
   N.x = 0.0f; N.y = 0.0f; N.z = 0.0f;
   D = 0.0f;
   return pl;
  };

}

and when I do this:

 plane plane_test;
 Stdout.formatln("{}", plane_test.N.x);

it prints out a nan, when it should be 0F, ofcourse - what am I doing wrong? 
Mar 23 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
dominik wrote:
 I'm confused by this:
 
 struct vector3 {
 
   union {
    struct { float x, y, z; };
    struct { float r, g, b; };
    struct { float cell[3]; };
   };
 
 }
 
 struct plane {
 
   union {
    struct { vector3 N; float D; };
    struct { float cell[4]; };
   };
 
   plane opCall() {
    plane pl;
    N.x = 0.0f; N.y = 0.0f; N.z = 0.0f;
    D = 0.0f;
    return pl;
   };
Should be "static plane opCall".
 }
 
 and when I do this:
 
  plane plane_test;
  Stdout.formatln("{}", plane_test.N.x);
 
 it prints out a nan, when it should be 0F, ofcourse - what am I doing wrong? 
Besides making the above a static opCall, you may also need to explicitly call it. auto plane = plane(); I don't think D calls it for you automatically. --bb
Mar 23 2008
parent reply "dominik" <asd asd.com> writes:
it works now, thanks!

"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fs6rdm$23j4$1 digitalmars.com...
 Should be "static plane opCall".
could you please explain why it should be static?
Mar 23 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
dominik wrote:
 it works now, thanks!
Great. Did you need to add an explicit call to it?
 
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fs6rdm$23j4$1 digitalmars.com...
 Should be "static plane opCall".
could you please explain why it should be static?
Because you don't have any instance at the time you're calling it. Maybe you're thinking of it like the equivalent of a constructor. It's not. It's just a hack that's been somewhat canonicalized by the language. It's just a factory function*. You could call it "create" and invoke it as "plane.create()". You could call it anything you want, but it needs to be static. Or you could move it outside the class and call it "createPlane". [*] Except that D will automatically call a single-argument static opCall if an initializer matches the opCall's argument. So if for some reason you had static plane opCall(string x) { ... } and did plane foo = "hi there"; that would call your static opCall(string) method. Otherwise static opCall is just an ordinary static method. --bb
Mar 23 2008
parent "dominik" <asd asd.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fs7209$2jld$1 digitalmars.com...
 dominik wrote:
 it works now, thanks!
Great. Did you need to add an explicit call to it?
yes I did, it works however :)
 Because you don't have any instance at the time you're calling it. Maybe 
 you're thinking of it like the equivalent of a constructor.  It's not. 
 It's just a hack that's been somewhat canonicalized by the language.
thats what confused me I guess. I've seen that idiom over and over again in D Language without fully understanding why it is so. I guess I started to look at it as a class with constructor - while it's obviously not. I think I'll stick to struct for vectors, but I still need to figure out why/when structs and why/when classes in D.
 [*] Except that D will automatically call a single-argument static opCall 
 if an initializer matches the opCall's argument.  So if for some reason 
 you had
    static plane opCall(string x) { ... }
 and did
    plane foo = "hi there";
 that would call your static opCall(string) method.  Otherwise static 
 opCall is just an ordinary static method.
I like verbose code though, so I'll stick to plane foo = plane("hi there") as I don't like to write lots of comments in code :)
Mar 23 2008