traits

Scala, Extend object with a generic trait

拥有回忆 提交于 2019-11-26 21:50:45
问题 I'm using Scala and I want to extend a (singleton) object with a trait, which delivers a data structure and some methods, like this: trait Tray[T] { val tray = ListBuffer.empty[T] def add[T] (t: T) = tray += t def get[T]: List[T] = tray.toList } And then I'll would like to mix-in the trait into an object, like this: object Test with Tray[Int] But there are type mismatches in add and get : Test.add(1) // ... How can I'll get this to work? Or what is my mistake? 回答1: The problem is that you're

How do I create an instance of a trait in a generic method in scala?

人走茶凉 提交于 2019-11-26 21:19:44
问题 I'm trying to create an instance of a trait using this method val inst = new Object with MyTrait This works well, but I'd like to move this creation in to a generator function, ie. object Creator { def create[T] : T = new Object with T } I'm obviously going to need the manifest to somehow fix the type erasure problems, but before I get to this, I run in to 2 questions : Even with an implicit manifest, Scala still demands that T be a trait. How do I add a restriction to create[T] so that T is

Java 8 default methods as traits : safe?

有些话、适合烂在心里 提交于 2019-11-26 21:18:37
Is it a safe practice to use default methods as a poor's man version of traits in Java 8? Some claim it may make pandas sad if you use them just for the sake of it, because it's cool, but that's not my intention. It is also often reminded that default methods were introduced to support API evolution and backward compatibility, which is true, but this does not make it wrong or twisted to use them as traits per se. I have the following practical use case in mind: public interface Loggable { default Logger logger() { return LoggerFactory.getLogger(this.getClass()); } } Or perhaps, define a

Why would I implement methods on a trait instead of as part of the trait?

血红的双手。 提交于 2019-11-26 20:29:12
While trying to understand the Any trait better, I saw that it has an impl block for the trait itself . I don't understand the purpose of this construct, or even if it has a specific name. I made a little experiment with both a "normal" trait method and a method defined in the impl block: trait Foo { fn foo_in_trait(&self) { println!("in foo") } } impl dyn Foo { fn foo_in_impl(&self) { println!("in impl") } } impl Foo for u8 {} fn main() { let x = Box::new(42u8) as Box<dyn Foo>; x.foo_in_trait(); x.foo_in_impl(); let y = &42u8 as &dyn Foo; y.foo_in_trait(); y.foo_in_impl(); // May cause an

Collisions with other trait methods

梦想的初衷 提交于 2019-11-26 19:57:27
问题 How can I deal with traits with methods of same name? trait FooTrait { public function fooMethod() { return 'foo method'; } public function getRow() { return 'foo row'; } } trait TooTrait { public function tooMethod() { return 'too method'; } public function getRow() { return 'too row'; } } class Boo { use FooTrait; use TooTrait; public function booMethod() { return $this->fooMethod(); } } error, Fatal error: Trait method getRow has not been applied, because there are collisions with other

When to use val or def in Scala traits?

為{幸葍}努か 提交于 2019-11-26 19:46:25
I was going through the effective scala slides and it mentions on slide 10 to never use val in a trait for abstract members and use def instead. The slide does not mention in detail why using abstract val in a trait is an anti-pattern. I would appreciate it if someone can explain best practice around using val vs def in a trait for abstract methods A def can be implemented by either of a def , a val , a lazy val or an object . So it's the most abstract form of defining a member. Since traits are usually abstract interfaces, saying you want a val is saying how the implementation should do. If

PHP traits - defining generic constants

孤街醉人 提交于 2019-11-26 19:26:11
问题 What is the best way to define constants that may be used by a number of classes within a namespace? I'm trying to avoid too much inheritance, so extending base classes is not an ideal solution, and I'm struggling to find a good solution using traits. Is this in any way possible in PHP 5.4 or should a different approach be taken? I have the following situation: trait Base { // Generic functions } class A { use Base; } class B { use Base; } The problem is that it is not possible to define

Is it possible to access struct fields from within a trait?

假装没事ソ 提交于 2019-11-26 19:08:58
Traits are used to group some functions to be implemented from a struct, but is it possible to access struct fields from within the trait? I could imagine declaring fields inside the trait so that the fields are abstracted as well. I haven't found such a syntax; is there any other solution? Otherwise, it wouldn't be possible to have non-static methods using a trait, would it? I know object oriented programming from C# and I'm playing around with Rust, trying to adapt the OOP functionality I already know from C#. DK. This sounds like you're misunderstanding how traits work. Traits can't have

How do I return an instance of a trait from a method?

[亡魂溺海] 提交于 2019-11-26 19:07:53
I'm trying to create a function that returns an instance of the Shader trait. Here is my drastically simplified code: trait Shader {} struct MyShader; impl Shader for MyShader {} struct GraphicsContext; impl GraphicsContext { fn create_shader(&self) -> Shader { let shader = MyShader; shader } } fn main() {} However I receive the following error: error[E0277]: the trait bound `Shader + 'static: std::marker::Sized` is not satisfied --> src/main.rs:10:32 | 10 | fn create_shader(&self) -> Shader { | ^^^^^^ `Shader + 'static` does not have a constant size known at compile-time | = help: the trait

How to mix-in a trait to instance?

那年仲夏 提交于 2019-11-26 19:02:24
问题 Given a trait MyTrait : trait MyTrait { def doSomething = println("boo") } it can be mixed into a class with extends or with : class MyClass extends MyTrait It can also be mixed upon instantiating a new instance: var o = new MyOtherClass with MyTrait o.doSomething But...can the trait (or any other if that makes a difference) be added to an existing instance? I'm loading objects using JPA in Java and I'd like to add some functionality to them using traits. Is it possible at all? I'd like to be