traits

If `Into<String>` is not implemented for `&String`, why are these implementations conflicting?

半城伤御伤魂 提交于 2019-12-10 14:44:09
问题 I asked a relevant question about why there is no implementation of From<&String> for String . I now want to create my own trait as the following: #[derive(Debug)] struct MyStruct(String); impl MyStruct { fn new<T>(t: T) -> MyStruct where T: MyIntoString, { MyStruct(t.my_into()) } } trait MyIntoString { fn my_into(self) -> String; } impl<'a> MyIntoString for &'a String { fn my_into(self) -> String { self.clone() } } impl<I> MyIntoString for I where I: Into<String>, { fn my_into(self) ->

The trait `A` is not implemented for the type `A`

送分小仙女□ 提交于 2019-12-10 14:41:42
问题 I am trying to use a trait that has a function that takes a closure as argument, and then use it on a trait object. trait A { fn f<P>(&self, p: P) where P: Fn() -> (); } struct B { a: Box<A> } impl B { fn c(&self) { self.a.f(|| {}); } } This snippet generates the following error: the trait `A` is not implemented for the type `A` [E0277] The version of rustc is rustc 1.0.0-beta.3 (5241bf9c3 2015-04-25) (built 2015-04-25) . 回答1: The problem is that method f is not object-safe because it is

Why do I need semicolons after these imports?

泪湿孤枕 提交于 2019-12-10 14:38:44
问题 I never really used Traits much in Scala so far, and I want to change this. I have this code: import tools.nsc.io.Path import java.io.File trait ImageFileAcceptor extends FileAcceptor { override def accept(f:File) = { super.accept(f) match { case true => { // additional work to see if it's really an image } case _ => false } } } The problem is, when I compile with sbt , I keep receiving: ImageFileAcceptor.scala:2: ';' expected but 'import' found. If I add ; after the imports, the code

How to “dereference a type” in C++03?

六眼飞鱼酱① 提交于 2019-12-10 14:34:52
问题 How do I get the "dereferenced type" of another type in C++03? Note that it can be other dereferenceable type like std::vector<int>::iterator . e.g. if I have template<typename T> struct MyPointer { T p; ??? operator *() { return *p; } }; How can I figure out what to replace the ??? with? ( No Boost ! I want to know how to figure it out myself.) 回答1: In the general case, you can't. For raw pointers, you can partially specialize as shown in other answers- custom smart pointers may have a

Abstract trait't method not allowed to be static in PHP?

不想你离开。 提交于 2019-12-10 14:00:49
问题 Here is my example: trait FileConfig { public static function getPathForUploads() { $paths = static::getPaths(); //etc. } abstract public static function getPaths(); //doesn't work. Error: "Static function SharedDefaultConfig::getPaths() should not be abstract" abstract public function getPaths(); //OK public static function getPaths() {} //OK } Class: class AppConfig { use FileConfig; public static function getPaths() { return array(...); } } Call: AppConfig::getPathForUploads(); It's

Implement Debug trait for large array type

感情迁移 提交于 2019-12-10 12:43:05
问题 I gather that Rust provides Debug impl's for arrays size 32 and smaller. I also gather that I could implement Debug on a larger array by simply using write! with a very long format specifier. But I'm wondering if there's a better way. What is the recommended method for implementing Debug for an array of length, say, 1024? 回答1: use std::fmt; struct Array<T> { data: [T; 1024] } impl<T: fmt::Debug> fmt::Debug for Array<T> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { self.data

how to get the size of the padding at the end of a class

感情迁移 提交于 2019-12-10 11:44:24
问题 I have a class A like this: struct V { virtual void f() = 0; }; struct A { int data; }; struct B : public A , public V { void f() override {} }; MSVC gives me sizeof(A) == 4 and sizeof(B) == 16 on a 64 bit build instead of 12 ( sizeof(void*) + sizeof(A) ) - so there is a 4 byte padding. Is there a way to get that padding? perhaps with some trait? The reason I need this is to do an assert like this: static_assert(sizeof(B) == sizeof(A) + std::is_polymorphic<camera>::value * sizeof(void*));

adding nested HasTraits properties to a TraitsUI TView

天涯浪子 提交于 2019-12-10 11:42:18
问题 i have a main HasTraits class which contains several Instance's of other HasTraits objects. I would like to define an Item in the view of the main object which points to a trait of a nested object. for example: class Person(HasTraits): name = String() class Pet(HasTraits): name = String() class Family(HasTraits): father = Instance(Person,()) dog = Instance(Pet,()) view = View( Item('father.name'), Item('dog.name'), ) is this possible? thanks! 回答1: Somebody named Alex asked this question 1

Why could we use traits compiled with 2.11 from 2.12?

假如想象 提交于 2019-12-10 11:40:03
问题 While understanding this answer I wrote my own example which uses a trait with scala-library version 2.11.4 : trait Trace { def id(): Int def concrete(i : Int) = println(i) } from a program uses version 2.12.0-M5 . object App { def main(args: Array[String]) = { println("st") val t = new TraceImpl println(t.id()) t.concrete(10) } class TraceImpl extends Trace{ override def id(): Int = 1 } } I expected that some exception would be thrown at runtime, but the program works fine and prints: st 1

DRY: how to use this code in several entities accross Symfony2 project? Traits?

孤街浪徒 提交于 2019-12-10 10:52:17
问题 I have this repetitive piece of code that will be used in more than one entity in my Symfony2 project so will be fine to apply some kind of DRY, if it's possible of course, and I'm thinking in PHP Traits. private static $preDeletedEntities;// static array that will contain entities due to deletion. private static $deletedEntities;// static array that will contain entities that were deleted (well, at least the SQL was thrown). /** * This callback will be called on the preRemove event * @ORM