Tuesday, 15 March 2011

types - How to pass the 'self' value of a method to another method? -


I am trying to do something similar to: Pass the 'self' value in the hope of getting the method. Calling ( get_dot ) is a kind of mismatch for accessing values ​​like x and y and I'm not sure that I Difference or something is required here is an example in which I pass passport to @ or this and the second method is used by my values Programming is able to properly use:

  class Test Manufacturer: - & gt; @x = 10 doSomething: (value) - & gt; Return @ x * value X doSomething2: () - & gt; @doSomething (@) y = New Test () warning (y.doSomething2 ()) // 100  

My real junk code looks like this:

  Struct Vec2 {x: f32, y: f32}} Encrypt Vec2 {// Other stuff fn get_dot (& self, right: Vec2) - & gt; F32 {self.x * right.x + self.y * right.y} fn get_magnitude (& self) - & gt; F32 {(auto .get_dot (auto)). Sqrt () // Problematic Line! }}  

I get the following error:

  src / vec2.rs: 86: 23: 86:27 error: mismatch type: expected vc 2 `, has been found & amp; Vec2` (expected straight 'Vec2`, found & amp; -ptr) [E0308] src / vec2.rs: 86 (auto .get_dot (self)). Sqrt () ^ ~~~ Error: aborting 1-character due to previous code  

< P> 1-character for your code is fixed:

  struct Vec2 {x: f32, y: f32,} impl vec2 {// other stuff fn get_dot (& amp; self, correct : & Amp; Vec2) - & gt; F32 {// Correct type self.x * right.x + self.y * right.y} Fn get_magnitude (& self) - & gt; F32 {(own .get_dot (self)). Sqrt ()}}  

The problem is that your get_dot method instead assumes the second argument instead the context. It is unnecessary (because the method does not need to be the owner of that argument, just do not need to be able to access it), and if you want to call it, it really can not work. get_magnitude .


No comments:

Post a Comment