traits

Difference between @Delegate, @Mixin and Traits in Groovy?

只谈情不闲聊 提交于 2019-11-26 18:53:43
问题 Would someone explain when I would want to use Groovy Traits vs. Mixins (@Mixin) vs. Delegates (@Delegate)? Maybe some trade-offs and design concerns would help. They all seem to allow for reusing multiple "classes" of behavior. Thanks. :-) This SO thread was helpful too: Difference between @Delegate and @Mixin AST transformations in Groovy 回答1: I agree, they all seem to allow reusing multiple "classes" of behaviour. There are differences, though, and understanding these will probably aid

How to override trait function and call it from the overridden function?

点点圈 提交于 2019-11-26 18:10:36
Scenario: trait A { function calc($v) { return $v+1; } } class MyClass { use A; function calc($v) { $v++; return A::calc($v); } } print (new MyClass())->calc(2); // should print 4 This code doesn't work, and I cannot find a way to call a trait function like it was inherited. I tried calling self::calc($v) , static::calc($v) , parent::calc($v) , A::calc($v) and the following: trait A { function calc($v) { return $v+1; } } class MyClass { use A { calc as traitcalc; } function calc($v) { $v++; return traitcalc($v); } } Nothing works. Is there a way to make it work or must I override completely

Mixins vs. Traits

喜你入骨 提交于 2019-11-26 17:54:39
问题 What is the difference between Mixins and Traits? According to Wikipedia, Ruby Modules are sort of like traits. How so? 回答1: Mixins may contain state, (traditional) traits don't. Mixins use "implicit conflict resolution", traits use "explicit conflict resolution" Mixins depends on linearization, traits are flattened. Lecture about traits ad 1. In mixins you can define instance variables. Traits do not allow this. The state must be provided by composing class (=class using the traits) ad 2.

How to clone a struct storing a boxed trait object?

孤街醉人 提交于 2019-11-26 17:50:19
I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box<Animal> . trait Animal { fn speak(&self); } struct Dog { name: String, } impl Dog { fn new(name: &str) -> Dog { return Dog { name: name.to_string(), }; } } impl Animal for Dog { fn speak(&self) { println!{"{}: ruff, ruff!", self.name}; } } struct AnimalHouse { animal: Box<Animal>, } fn main() { let house = AnimalHouse { animal: Box::new(Dog::new("Bobby")), }; house.animal.speak(); } It returns "Bobby: ruff, ruff!" as expected, but if I

Find out whether a C++ object is callable

不打扰是莪最后的温柔 提交于 2019-11-26 17:30:27
Is it possible to write a type trait, say is_callable<T> which tells if an object has an operator() defined? It is easy if the arguments to the call operator are known in advance, but not in the general case. I want the trait to return true if and only if there is at least one overloaded call operator defined. This question is related and has a good answer, but it doesn't work on all types (only on int -convertible types). Also, std::is_function works, but only on proper C++ functions, not on functors. I'm looking for a more general solution. jrok I think this trait does what you want. It

Conflicting implementations of trait in Rust

浪子不回头ぞ 提交于 2019-11-26 17:23:24
问题 I want to implement a custom trait for &'a str and for integer numbers up to i32 , but Rust does not allow me to: use std::convert::Into; pub trait UiId { fn push(&self); } impl<'a> UiId for &'a str { fn push(&self) {} } impl<T: Into<i32>> UiId for T { fn push(&self) {} } fn main() {} This fails to compile with the following error: error[E0119]: conflicting implementations of trait `UiId` for type `&str`: --> src/main.rs:11:1 | 7 | impl<'a> UiId for &'a str { | ------------------------- first

Scala single method interface implementation

安稳与你 提交于 2019-11-26 17:13:29
问题 Does Scala have any syntactic sugar to replace the following code: val thread = new Thread(new Runnable { def run() { println("hello world") } }) with something more like: val thread = new Thread(() => println("hello world")) in cases when the trait/interface needs only one method to be implemented? If not, is there any chance to have this feature in Scala in the future? It is especially useful when one deals with Java classes. I found a similar question asked three years ago: Generically

Why can a Scala trait extend a class?

不羁岁月 提交于 2019-11-26 15:43:45
问题 I see that traits in Scala are similar to interfaces in Java (but interfaces in Java extend other interfaces, they don't extend a class). I saw an example on SO about traits usage where a trait extends a class. What is the purpose of this? Why can traits extend classes? 回答1: Yes they can, a trait that extends a class puts a restriction on what classes can extend that trait - namely, all classes that mix-in that trait must extend that class . scala> class Foo defined class Foo scala> trait

How do I use integer number literals when using generic types?

二次信任 提交于 2019-11-26 15:32:42
I wanted to implement a function computing the number of digits within any generic type of integer. Here is the code I came up with: extern crate num; use num::Integer; fn int_length<T: Integer>(mut x: T) -> u8 { if x == 0 { return 1; } let mut length = 0u8; if x < 0 { length += 1; x = -x; } while x > 0 { x /= 10; length += 1; } length } fn main() { println!("{}", int_length(45)); println!("{}", int_length(-45)); } And here is the compiler output error[E0308]: mismatched types --> src/main.rs:5:13 | 5 | if x == 0 { | ^ expected type parameter, found integral variable | = note: expected type `T

References to traits in structs

老子叫甜甜 提交于 2019-11-26 15:16:32
问题 I have a trait Foo pub trait Foo { fn do_something(&self) -> f64; } and a struct which references that trait pub struct Bar { foo: Foo, } Trying to compile I get error: reference to trait `Foo` where a type is expected; try `Box<Foo>` or `&Foo` Changing the struct to struct Bar { foo: &Foo, } Tells me error: missing lifetime specifier Changing the definition to struct Bar { foo: Box<Foo>, } Compiles — yay! However, when I want a function to return foo on bar - something like: impl Bar { fn