traits

Resolving trait implementation conflicts

时光毁灭记忆、已成空白 提交于 2019-12-05 09:00:46
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 side. My first idea was to write a trait Dot1 for a binary function to replace Mul in that it also

Is there a trait supplying `iter()`?

假装没事ソ 提交于 2019-12-05 08:09:53
Is there in Rust a Trait which supplies the iter() method? I only found the trait IntoIterator , which supplies into_inter() . Just to be clear here: I do not want the Iterator trait, which supplies next() , but a trait which supplies iter() . [ sidenote: Sometimes I'm very confused by the Rust libs. Iterator supplies next() , but IntoIterator supplies into_iter() (not supplying next() and for convention with moving), while IntoIter is a struct, that implements the Iterator trait (moving values). ] No, there is no trait that provides iter() . However, IntoIterator is implemented on references

How to solve “Implementation restriction: trait … accesses protected method … inside a concrete trait method.”

只愿长相守 提交于 2019-12-05 07:50:12
A Java library class I'm using declares protected getPage(): Page { ... } Now I want to make a helper Scala mixin to add features that I often use. I don't want to extend the class, because the Java class has different subclasses I want to extend at different places. The problem is that if I use getPage() in my mixin trait , I get this error: Implementation restriction: trait MyMixin accesses protected method getPage inside a concrete trait method. Is there a solution how to make it work, without affecting my subclasses? And why is there this restriction? So far, I came up with a work-around:

Symfony container traits

醉酒当歌 提交于 2019-12-05 07:13:31
Strange problem, I have controller which uses \Symfony\Component\DependencyInjection\ContainerAwareTrait class MainController { use \Symfony\Component\DependencyInjection\ContainerAwareTrait; /** * @Route("/", name="_index") * @Template() */ public function indexAction() { var_dump($this->container); return array(); } } but result is NULL. Tried on: Symfony 2.5.* MAMP 3.0 PHP 5.4 5.5 My searches have not helped me. I think the solution is easy. Any ideas how to trace this error? UPD: When i extend from Controller, container is available and everything is working properly. But according to

How to implement a trait for any mutability?

你离开我真会死。 提交于 2019-12-05 07:06:48
Can mutability be a generic parameter in traits? I'd like to implement a trait for a mutable and an immutable variant of a type without having to copy&paste the impl block. trait Foo<T> {…} impl<T> Foo for *const T {…} impl<T> Foo for *mut T {…same thing again…} Wishful pseudocode: trait Foo<T> {…} impl<T, Mutability> Foo for *Mutability T {…} Can mutability be a generic parameter in traits? No. ^_^ Here's some detailed discussion on the matter ( Internals , Reddit ). I think in general people recognize that the current state is not ideal, but that it's not terribly constraining at the moment,

How can I fire a Traits static event notification on a List?

空扰寡人 提交于 2019-12-05 06:58:36
I am working through the traits presentation from PyCon 2010 . At about 2:30:45 the presenter starts covering trait event notifications , which allow (among other things) the ability to automatically call a subroutine any time a trait has changed. I am running a modified copy of the example he gave... In this trial, I am trying to see whether I can fire a static event whenever I make a change to volume or inputs . from traits.api import HasTraits, Range, List, Float import traits class Amplifier(HasTraits): """ Define an Amplifier (a la Spinal Tap) with Enthought's traits. Use traits to

Using traits with a factory

核能气质少年 提交于 2019-12-05 06:43:18
I'm currently discovering scala and I was wondering if I could use traits with a factory. I tried this : abstract class Foo { ... } object Foo { def apply() = new Bar private class Bar extends Foo { ... } } Foo() with MyTrait // Not working I guess it's because with must be preceded by new . So is there any way to do this ? Thank you No it is too late, the instance is already created when the apply() method returns. What you can do is using the traits inside the factory method. The code below is from a rather big code example I am writing: object Avatar { // Avatar factory method def apply

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

让人想犯罪 __ 提交于 2019-12-05 02:16:03
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, { let len = file.write(b"1234")?; Ok(len) } fn read_4_bytes<R>(mut file: R) -> Result<[u8; 4], io::Error

Implementing Ord for a type is awkward?

偶尔善良 提交于 2019-12-05 01:56:46
I have a newtype and I want to implement Ord : use std::cmp::{Ord, Ordering}; struct MyType(isize); impl Ord for MyType { fn cmp(&self, &other: Self) -> Ordering { let MyType(ref lhs) = *self; let MyType(ref rhs) = *other; lhs.cmp(rhs) } } When I try to compare two variables of my types, I get errors: error[E0277]: the trait bound `MyType: std::cmp::PartialOrd` is not satisfied --> src/main.rs:5:6 | 5 | impl Ord for MyType { | ^^^ can't compare `MyType` with `MyType` | = help: the trait `std::cmp::PartialOrd` is not implemented for `MyType` error[E0277]: the trait bound `MyType: std::cmp::Eq`

PHP - Traits as helpers

拈花ヽ惹草 提交于 2019-12-04 23:59:07
问题 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); } } 回答1: 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. 回答2: Traits have been added to PHP for a very simple reason: PHP does not support