traits

Is it possible to extend a default method implementation of a trait in a struct?

你说的曾经没有我的故事 提交于 2019-11-27 22:59:22
In traditional object-oriented languages (e.g. Java), it is possible to "extend" the functionality of a method in an inherited class by calling the original method from the super class in the overridden version, for example: class A { public void method() { System.out.println("I am doing some serious stuff!"); } } class B extends A { @Override public void method() { super.method(); // here we call the original version System.out.println("And I'm doing something more!"); } } As you can see, in Java, I am able to call the original version from the super class using the super keyword. I was able

Extend Traits with Classes in PHP?

两盒软妹~` 提交于 2019-11-27 21:53:22
Why we are not allowed to extend Traits with Classes in PHP? For example: Trait T { } Class C use T {} /* or */ Class C extends T {} Is there any potential conflict for such syntax? I do not think so. The PHP manual states thus: Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces

Why is the `Sized` bound necessary in this trait?

ぐ巨炮叔叔 提交于 2019-11-27 20:11:49
I have a trait with two associated functions: trait WithConstructor: Sized { fn new_with_param(param: usize) -> Self; fn new() -> Self { Self::new_with_param(0) } } Why does the default implementation of the second method ( new() ) force me to put the Sized bound on the type? I think it's because of the stack pointer manipulation, but I'm not sure. If the compiler needs to know the size to allocate memory on the stack, why does the following example not require Sized for T ? struct SimpleStruct<T> { field: T, } fn main() { let s = SimpleStruct { field: 0u32 }; } Vladimir Matveev As you

How to overload class constructor within traits in PHP >= 5.4

心已入冬 提交于 2019-11-27 18:27:20
In PHP 5, I can to overload constructors (and any others methods). But if I get some code like this: class Base { public function __construct($a, $b) { echo $a+$b; } public function sayHello() { echo 'Hello '; } } trait SayWorld { public function __construct($a, $b, $c = 0) { echo (int)$c * ($a+$b); } public function sayHello($a = null) { parent::sayHello(); echo 'World!'.$a; } } class MyHelloWorld extends Base { use SayWorld; } $o = new MyHelloWorld(2, 3); $o->sayHello(1); I have an error: Fatal error: MyHelloWorld has colliding constructor definitions coming from traits How can I to fix it?

PHP traits - defining generic constants

 ̄綄美尐妖づ 提交于 2019-11-27 18:17:57
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 constants in PHP traits. Ideally, I would want something like the following: trait Base { const SOME_CONST

How to mix-in a trait to instance?

心不动则不痛 提交于 2019-11-27 17:30:34
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 able to mix in a trait as follows: var o = DBHelper.loadMyEntityFromDB(primaryKey); o = o with MyTrait

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

岁酱吖の 提交于 2019-11-27 17:03:17
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 Steinar I agree, they all seem to allow reusing multiple "classes" of behaviour. There are differences, though, and understanding these will probably aid your decision. Before providing a brief summary/highlight of each feature and examples of suitable

Conflicting implementations of trait in Rust

扶醉桌前 提交于 2019-11-27 16:04:03
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 implementation here ... 11 | impl<T: Into<i32>> UiId for T { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Refactoring legacy mixin-based class hierarchies

▼魔方 西西 提交于 2019-11-27 15:54:35
I'm currently working on a huge javascript project which has a huge class hierarchy and heavily uses mixins to extend functionality of base classes. Here is an example of how mixin looks like, we're using compose library to create class-like objects: // Base.js var Base = compose({ setX: function (x) { this.x = x; }, setY: function (y) { this.y = y; }, setPosition: function (x, y) { this.setX(x); this.setY(y); } }) // SameXAndY.js - mixin var SameXAndY = compose({ // Executes after setX in Base.js setX: compose.after(function (x) { this.y = x; }), // Executes after setY in Base.js setY:

Scala single method interface implementation

喜夏-厌秋 提交于 2019-11-27 15:27:57
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 implementing a Java Single-Abstract-Method interface with a Scala closure? The answer says we should have