traits

PHP Traits: How to resolve a property name conflict?

守給你的承諾、 提交于 2019-12-22 07:58:08
问题 How to resolve a property name conflict when a class uses two Traits with homonymous properties ? Example: <?php trait Video { public $name = 'v'; } trait Audio { public $name = 'a'; } class Media { use Audio, Video; } $media = new Media(); $media->name; I've tried insteadof ( Video::name insteadof Audio ) and ( Video::name as name2 ) without success. Thanks in advance ! 回答1: You can't, its for methods only. However they may use the same property name only if the value is the same: trait

Resolving trait implementation conflicts

左心房为你撑大大i 提交于 2019-12-22 06:02:16
问题 I'm trying to write some generic math functions in Rust and I keep running into the following error message: error: conflicting implementations for trait SoAndSo Is it possible to solve the problem? If so, how? For example, I'm trying to write a generic dot product that takes two iterators, zips them and iterates over the pairs to accumulate the products. I want this function also to be able to compute complex-valued dot products. The dot product over complex numbers involves conjugating one

Resolving trait implementation conflicts

五迷三道 提交于 2019-12-22 06:01:09
问题 I'm trying to write some generic math functions in Rust and I keep running into the following error message: error: conflicting implementations for trait SoAndSo Is it possible to solve the problem? If so, how? For example, I'm trying to write a generic dot product that takes two iterators, zips them and iterates over the pairs to accumulate the products. I want this function also to be able to compute complex-valued dot products. The dot product over complex numbers involves conjugating one

What is the difference between &Trait and impl Trait when used as method arguments?

旧时模样 提交于 2019-12-22 04:52:47
问题 In my project so far, I use many traits to permit mocking/stubbing in unit tests for injected dependencies. However, one detail of what I'm doing so far seems so suspicious that I'm surprised it even compiles. I'm worried that something dangerous is going on that I don't see or understand. It's based on the difference between these two method signatures: fn confirm<T>(subject: &MyTrait<T>) ... fn confirm<T>(subject: impl MyTrait<T>) ... I only just discovered the impl ... syntax in method

What is the difference between &Trait and impl Trait when used as method arguments?

那年仲夏 提交于 2019-12-22 04:52:08
问题 In my project so far, I use many traits to permit mocking/stubbing in unit tests for injected dependencies. However, one detail of what I'm doing so far seems so suspicious that I'm surprised it even compiles. I'm worried that something dangerous is going on that I don't see or understand. It's based on the difference between these two method signatures: fn confirm<T>(subject: &MyTrait<T>) ... fn confirm<T>(subject: impl MyTrait<T>) ... I only just discovered the impl ... syntax in method

Scala traits mixin order and super call

谁都会走 提交于 2019-12-22 04:45:07
问题 I have this code: trait base{ def msg: Unit= { println{"base"} } } trait foo extends base { abstract override def msg: Unit ={ super.msg println("foo") } } class base2{ def msg:Unit = { println{"base 2"} } } class test extends base2 with foo{ override def msg: Unit ={ super.msg println("done") } } If I call (new test).msg , this prints out things like: base, foo, done However, if I change the base trait to: trait base{ def msg: Unit } it prints out things like: base 2, foo, done I understand

How to read (std::io::Read) from a Vec or Slice?

纵饮孤独 提交于 2019-12-22 04:09:20
问题 Vec s support std::io::Write , so code can be written that takes a File or Vec , for example. From the API reference, it looks like neither Vec nor slices support std::io::Read . Is there a convenient way to achieve this? Does it require writing a wrapper struct? Here is an example of working code, that reads and writes a file, with a single line commented that should read a vector. use ::std::io; // Generic IO fn write_4_bytes<W>(mut file: W) -> Result<usize, io::Error> where W: io::Write, {

Does Rust have Collection traits?

▼魔方 西西 提交于 2019-12-22 03:24:28
问题 I'd like to write a library that's a thin wrapper around some of the functionality in BTreeMap. I'd prefer not to tightly couple it to that particular data structure though. Strictly speaking, I only need a subset of its functionality, something along the lines of the NavigableMap interface in Java. I was hoping to find an analogous trait I could use. I seem to recall that at some point there were traits like Map and MutableMap in the standard library, but they seem to be absent now. Is there

Mixin or Trait implementation in AS3?

天涯浪子 提交于 2019-12-21 17:19:45
问题 I'm looking for ideas on how to implement a Mixin/Trait style system in AS3. I want to be able to compose a number of classes together into a single object. Of course this is not a language level feature of AS3, but I'm hoping that there is maybe some way to do this using prototype based techniques or maybe some bytecode hacking that I believe AsMock uses to implement it's functionality. An existing Java example is Qi4J where the user define interfaces that the Qi4j framework implements based

How does the Scala compiler handle concrete trait methods?

ぃ、小莉子 提交于 2019-12-21 08:07:05
问题 If I have the following Scala class: abstract class MyOrdered extends Ordered[MyOrdered] { def id: Int def compare(that : MyOrdered) : Int = if (that==null) 1 else (id-that.id) } Then I only need to define the id method in Scala to get a concrete class. But if I try to extend it in Java , the compiler says that all the concrete methods of Ordered are missing. So, does that mean that the Scala compiler is only putting the implementation of the concrete methods of Ordered in concrete Scala