traits

Get all the classes that implements a trait in Scala using reflection

本小妞迷上赌 提交于 2019-11-27 15:18:32
问题 I want to list out all the case classes which implements a particular trait. I am currently using Clapper ClassUtil for doing that. I am able to get the case classes that are directly implementing a trait. However, I am not able to get the other classes which are not directly implementing the trait. How can I get all classes which directly or indirectly implements a trait. ? val finder = ClassFinder() finder.getClasses().filter(_.isConcrete).filter(_.implements("com.myapp.MyTrait")) Scala

Trait implementation for both a trait object and for direct implementors of the trait

↘锁芯ラ 提交于 2019-11-27 15:17:13
I have a struct that mostly encapsulates a vector: struct Group<S> { elements: Vec<S> } I also have a simple trait which is also implemented for other structs: trait Solid { fn intersect(&self, ray: f32) -> f32; } I want to implement Solid for Group , but I want to be able to use Group both for lists of the same implementation of Solid and for lists of mixed implementations of Solid . Basically I want to use both Group<Box<Solid>> and Group<Sphere> ( Sphere implements Solid ). Currently I am using something like this: impl Solid for Group<Box<Solid>> { fn intersect(&self, ray: f32) -> f32 { /

What does “trait A <: B” mean?

爱⌒轻易说出口 提交于 2019-11-27 14:39:48
问题 In Scala, what does trait A <: B mean? Is it just the same as trait A extends B ? Edited to add: I'm familiar with the syntax for type parameters, and what <: means in that context. However, in the above example it would seem to me that A is the name of the trait being declared, not a type parameter. 回答1: NOTE As of Scala 2.12.5 using <: for extends is deprecated scala -deprecation -e 'trait B; trait A <: B' /var/folders/0w/kb0d3rqn4zb9fcc91pxhgn8w0000gn/T/scalacmd2374381600671257557.scala:1:

Building a Singleton Trait with PHP 5.4

允我心安 提交于 2019-11-27 12:15:32
问题 We recently had a discussion if it was possible to build a trait Singleton PHP Traits and we played around with it a possible Implementation but ran into issues with building one. This is an academic exercise. I know that Singletons have very little - if not to say no - use in PHP and that one should 'just create one' but just for exploring the possibilities of traits: <?php trait Singleton { protected static $instance; final public static function getInstance() { return isset(static::

Why can a Scala trait extend a class?

拥有回忆 提交于 2019-11-27 11:38:44
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? adelbertc 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 FooTrait extends Foo defined trait FooTrait scala> val good = new Foo with FooTrait good: Foo with

References to traits in structs

放肆的年华 提交于 2019-11-27 10:43:54
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 get_foo(&self) -> Foo { self.foo } } Well obviously bar.foo is a Box<Foo> , so expectedly I get error:

Mixins vs. Traits

三世轮回 提交于 2019-11-27 10:04:20
What is the difference between Mixins and Traits? According to Wikipedia , Ruby Modules are sort of like traits. How so? 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. There may be the name conflict. Two mixins ( MA and MB ) or traits ( TA and TB ) define method with the same

What is the difference between traits in Rust and typeclasses in Haskell?

做~自己de王妃 提交于 2019-11-27 09:20:44
问题 Traits in Rust seem at least superficially similar to typeclasses in Haskell, however I've seen people write that there are some differences between them. I was wondering exactly what these differences are. 回答1: At the basic level, there's not much difference, but they're still there. Haskell describes functions or values defined in a typeclass as 'methods', just as traits describe OOP methods in the objects they enclose. However, Haskell deals with these differently, treating them as

Can't implement a trait I don't own for all types that implement a trait I do own

一世执手 提交于 2019-11-27 08:36:51
问题 pub trait AllValues { fn all_values() -> Vec<Self> where Self: std::marker::Sized; } use rand::Rand; use rand::Rng; impl<T: AllValues + Sized> Rand for T { fn rand<R: Rng, T>(rng: &mut R) -> T { let values = T::all_values(); let len = values.len(); if len == 0 { panic!("Cannot pick a random value because T::all_values() returned an empty vector!") } else { let i = rng.gen_range(0, len); values[i] } } } The preceding code produces the following compile-time error: error[E0210]: type parameter

Understanding Traits and Object Safety

微笑、不失礼 提交于 2019-11-27 08:06:24
问题 I am struggling with the basics of object safety. If I have this code struct S { x: i32 } trait Trait: Sized { fn f(&self) -> i32 where Self: Sized; } fn object_safety_dynamic(x: Trait) {} I receive fn object_safety_dynamic(x: Trait) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `traits::Trait` cannot be made into an object = note: the trait cannot require that `Self : Sized` When adding / changing :Sized as the trait's inheritance or f 's bound I receive slightly different error messages.