traits

Compiler forces me to implement trait method but the `Self` trait bound on method is never satisfied for my type

℡╲_俬逩灬. 提交于 2019-12-04 00:42:31
问题 I have a trait Foo . I want to force implementors to define a method, if those implementors implement another trait ( Clone in this example). My idea (Playground): trait Foo { // Note: in my real application, the trait has other methods as well, // so I can't simply add `Clone` as super trait fn foo(&self) where Self: Clone; } struct NoClone; impl Foo for NoClone {} Sadly, this leads to: error[E0046]: not all trait items implemented, missing: `foo` --> src/lib.rs:8:1 | 2 | / fn foo(&self) 3 |

PHP type-hinting traits

十年热恋 提交于 2019-12-03 23:44:55
I have a trait. For the sake of creativity, let's call this trait Trait: trait Trait{ static function treat($instance){ // treat that trait instance with care } } Now, I also have a class that uses this trait, User. When trying to call treat with an instance of User, everything works. But I would like to type-hint that only instances of classes using Trait should be given as arguments, like that: static function treat(Trait $instance){...} Sadly, though, this results in a fatal error that says the function was expecting an instance of Trait, but an instance of User was given. That type of type

When extending a trait within a trait, what does 'super' refer to?

只愿长相守 提交于 2019-12-03 23:28:08
I want to to extend a trait within a trait, like this: trait NodeTypes { trait Node { def allNodesHaveThis: Int } } trait ScrumptiousTypes extends NodeTypes { trait Node extends super.Node { def scrumptiousness: Int } } trait YummyTypes extends NodeTypes { trait Node extends super.Node { def yumminess: Int } } object Graph extends NodeTypes with ScrumptiousTypes with YummyTypes { case class Node() extends super.Node { override def allNodesHaveThis = 1 override def scrumptiousness = 2 // error: ScrumptiousTypes.Node has been disinherited override def yumminess = 3 } } If this works, it would be

Mysterious lifetime issue while implementing trait for dyn object

独自空忆成欢 提交于 2019-12-03 22:34:57
Consider the following toy example: use std::cmp::Ordering; pub trait SimpleOrder { fn key(&self) -> u32; } impl PartialOrd for dyn SimpleOrder { fn partial_cmp(&self, other: &dyn SimpleOrder) -> Option<Ordering> { Some(self.cmp(other)) } } impl Ord for dyn SimpleOrder { fn cmp(&self, other: &dyn SimpleOrder) -> Ordering { self.key().cmp(&other.key()) } } impl PartialEq for dyn SimpleOrder { fn eq(&self, other: &dyn SimpleOrder) -> bool { self.key() == other.key() } } impl Eq for SimpleOrder {} This doesn't compile. It claims there is a lifetime issue in the implementation for partial_cmp :

How to prevent use of trait methods out of “use” scope in PHP

放肆的年华 提交于 2019-12-03 17:27:57
I'd like to know if there is any way to prevent the use of trait methods out of any class context in PHP ? Let me explain what I want with a short example, here is my current code : // File : MyFunctions.php trait MyFunctions { function hello_world() { echo 'Hello World !'; } } // File : A.php include 'MyFunctions.php'; class A { use MyFunctions; } // File : testTraits.php include 'A.php'; hello_world(); // Call to undefined function -> OK, expected A::hello_world(); // Hello World ! -> OK, expected MyFunctions::hello_world(); // Hello World ! -> Maybe OK, but not expected, I'd like to prevent

PHP - Traits as helpers

杀马特。学长 韩版系。学妹 提交于 2019-12-03 16:03:56
Are there any contradictions to use traits to inject helper methods like this? class Foo { use Helper\Array; function isFooValid(array $foo) { return $this->arrayContainsOnly('BarClass', $foo); } } That's the idea with traits. However you should still keep an eye out for coupled code. If Helper\Array is a completely different namespace from what Foo is in you might want to re-think this particular approach. Traits have been added to PHP for a very simple reason: PHP does not support multiple inheritance. Simply put, a class cannot extends more than on class at a time. This becomes laborious

How does insteadof keyword in a Trait work

匆匆过客 提交于 2019-12-03 15:50:55
I was just reading about traits and how multiple php traits can be used in the same php code separated by commas. I do not however understand the concept of the insteadof keyword that is used to resolve conflict in case two traits have the same function. Can anyone please explain how insteadof keyword works and how to use it to tell the engine that I am willing to use function hello() of trait A instead of that of trait B, given there are two traits A and B and a function hello() in both the traits. Explanation According to Traits Documentation , when you have same method in multiple trait,

What are possible use scenarios for Traits in PHP? [duplicate]

别来无恙 提交于 2019-12-03 15:05:12
This question already has answers here : Possible Duplicate: traits in php – any real world examples/best practices? In what kind of situations would one use Traits in PHP? I do have a pretty good overall idea of this, but I can't seem to think of a way to use them in an application I have written, but that may be because it does not need traits at the time. One scenario I have realized that needs traits: Events. Instead of having one class that implements the observer pattern and letting all other classes inheriting it, just make it a trait and let classes that want to fire events or

How to make a variable private to a trait?

谁都会走 提交于 2019-12-03 12:44:56
问题 I'd like to reuse a functionality several times in a single class. This functionality relies on a private variable: trait Address { private $address; public function getAddress() { return $this->address; } public function setAddress($address) { $this->address = $address; } } The only way I've found to use the trait twice, is the following: class User { use Address { getAddress as getHomeAddress; setAddress as setHomeAddress; getAddress as getWorkAddress; setAddress as setWorkAddress; } } The

Scala initialization behaviour

六眼飞鱼酱① 提交于 2019-12-03 12:29:52
问题 Please look at the following code. trait MyTrait { val myVal : String } class MyClass extends MyTrait { val myVal = "Value" } class MyClass2(val myVal: String) extends MyTrait Why does the initialization order differ in case of MyClass and MyClass2 ? The constructor of MyClass will be as MyClass() { MyTrait$class.$init$(this); myVal = value } The constructor of MyClass2 will be MyClass2(String myVal) { this.myVal = myVal; MyTrait$class.$init$(this) } I think the initialization order should be