These are chat archives for rust-lang/rust

12th
Feb 2017
mhsjlw
@mhsjlw
Feb 12 2017 01:48
I also asked this in the IRC:
trait Noise {
  fn compute(&self, x: f64, z: f64) -> f64;
}
Say, I do that, but I want impls that have that to also implement other functions, how do I do that
for example, one noise has a new and lerp etc. while other things that implement Noise don't have those functions
mhsjlw
@mhsjlw
Feb 12 2017 02:00
hmm
this is tough
Alexander Ronald Altman
@pthariensflame
Feb 12 2017 02:05
Make subtraits of Noise?
mhsjlw
@mhsjlw
Feb 12 2017 02:13
?
Hmm
What about the new function
Hmm, actually, let me make a gist
so, what do you suggest for the new() function
the combined noise should be able to accept Perlin or Octave
that's my goal
Alexander Ronald Altman
@pthariensflame
Feb 12 2017 02:22
Create an associated type!
Working on your example in the i32 playground.
Also, you don’t need to store the count separately in Octave; the Vec already has that information for free.
mhsjlw
@mhsjlw
Feb 12 2017 02:30
Ah, ok.
Yeah, no idea what you mean @pthariensflame :P
This is how you do something like that.
If you don’t need the lerp and fade functions for anything other than PerlinNoise, they just become methods on the type itself.
Notice the use of the associated type to abstract over what kind of configuration data each implementation of Noise might need.
Alexander Ronald Altman
@pthariensflame
Feb 12 2017 02:39
Here is a slightly cleaned-up version of the same: http://play.integer32.com/?gist=90fda9087f757f8c6e62fac281652dd4&version=stable
mhsjlw
@mhsjlw
Feb 12 2017 02:42
@pthariensflame thank you very much.
I was unaware that you could type = Config
Alexander Ronald Altman
@pthariensflame
Feb 12 2017 02:43
It’s one of Rust’s more powerful features: https://doc.rust-lang.org/book/associated-types.html
It’s used to great effect all over the standard library.