traits

Substitution failure is not an error (SFINAE) for enum

a 夏天 提交于 2019-12-02 01:54:07
问题 Is there a way to use Substitution failure is not an error (SFINAE) for enum? template <typename T> struct Traits { } template <> struct Traits<A> { }; template <> struct Traits<B> { enum { iOption = 1 }; }; template <T> void Do() { // use Traits<T>::iOption }; Then, Do<B>(); works and Do<A>(); fails. However, I can supply a default behavior when iOption does not exist. So I separate out some part of Do to DoOption. template <typename T, bool bOptionExist> void DoOption() { // can't use

In Scala how can I advise my own methods?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 01:33:24
问题 I want to do this: trait Renderable { def render: String } trait Parens extends Renderable { abstract override def render = "(" + super.render + ")" } object Foo extends Renderable with Parens { def render = "Hello" } But this does not work because the linearization order puts Parens after Foo (Foo always comes, of course) so Parens can't advise Foo.render. I end up doing this: trait FooRender { def render = "Hello" } object Foo extends FooRender with Parens { } But sometimes I really don't

Can #[inline] be used in both trait method declarations and implementations?

梦想的初衷 提交于 2019-12-02 00:57:35
I have a trait with some small methods, which are generally implemented as one-line wrappers around other methods that the implementing structs have. If I want to make sure that the trait method is inlined, should I place #[inline(always)] inside the trait definition, or inside the impl for each struct? I'd prefer to simply put it in the trait definition, but as far as I can tell that doesn't work. Matthieu M. What does inline mean? When a compiler inlines a call, it copies the body of the function at the call site. Essentially, it's as if the code had been copy/pasted at each call site where

Can I cast between two traits?

落爺英雄遲暮 提交于 2019-12-02 00:49:02
问题 I swear I searched all the internet and I tried hard to understand all answers that I found that seemed related. However, I still fail to understand if this is possible or not. trait Foo { fn do_foo (&self); } trait Bar { fn do_bar (&self); } struct SomeFoo; impl Foo for SomeFoo { fn do_foo(&self) { println!("doing foo"); } } struct SomeFooBar; impl Foo for SomeFooBar { fn do_foo(&self) { println!("doing foo"); } } impl Bar for SomeFooBar { fn do_bar(&self) { println!("doing bar"); } } fn

How to avoid circular dependencies when setting Properties?

China☆狼群 提交于 2019-12-02 00:42:23
问题 This is a design principle question for classes dealing with mathematical/physical equations where the user is allowed to set any parameter upon which the remaining are being calculated. In this example I would like to be able to let the frequency be set as well while avoiding circular dependencies. For example: from traits.api import HasTraits, Float, Property from scipy.constants import c, h class Photon(HasTraits): wavelength = Float # would like to do Property, but that would be circular?

In Scala how can I advise my own methods?

故事扮演 提交于 2019-12-02 00:33:21
I want to do this: trait Renderable { def render: String } trait Parens extends Renderable { abstract override def render = "(" + super.render + ")" } object Foo extends Renderable with Parens { def render = "Hello" } But this does not work because the linearization order puts Parens after Foo (Foo always comes, of course) so Parens can't advise Foo.render. I end up doing this: trait FooRender { def render = "Hello" } object Foo extends FooRender with Parens { } But sometimes I really don't want to do that because it breaks things up. As far as I can tell, linearization order is the only thing

Cannot call rusqlite's query because it expects the type &[&rusqlite::types::ToSql]

陌路散爱 提交于 2019-12-02 00:08:09
I want to use a prepared statement with rusqlite . Rusqlite implements the trait ToSql for String , &str and a bunch of other types : extern crate rusqlite; use rusqlite::Connection; fn main() { let mut connection = Connection::open("C:\\test_db.db").unwrap(); let mut cached_statement = connection .prepare_cached("SELECT ?, ?, ? FROM test") .unwrap(); let vec_values = vec![ &"test1".to_string(), &"test2".to_string(), &"test3".to_string(), ]; let rows = cached_statement.query(vec_values.as_slice()).unwrap(); } This does not compile with the error: error[E0308]: mismatched types --> src/main.rs

Can I cast between two traits?

和自甴很熟 提交于 2019-12-01 21:50:18
I swear I searched all the internet and I tried hard to understand all answers that I found that seemed related. However, I still fail to understand if this is possible or not. trait Foo { fn do_foo (&self); } trait Bar { fn do_bar (&self); } struct SomeFoo; impl Foo for SomeFoo { fn do_foo(&self) { println!("doing foo"); } } struct SomeFooBar; impl Foo for SomeFooBar { fn do_foo(&self) { println!("doing foo"); } } impl Bar for SomeFooBar { fn do_bar(&self) { println!("doing bar"); } } fn main () { let foos:Vec<Box<Foo>> = vec!(Box::new(SomeFoo), Box::new(SomeFooBar)); for foo in foos { foo.do

How can I enforce equality of two associated type parameters of traits?

£可爱£侵袭症+ 提交于 2019-12-01 21:42:13
I have a function f which takes two arguments of the same type, and a function g which takes two arguments of different types, but both types have to store the same value, so that g can call f with the values contained in the arguments to f . I currently implemented something like this: fn f<T>(a: T, b: T) {} trait A { type A; fn getter(&self) -> Self::A; } fn g<T: A, U: A>(a: T, b: U) { f(a.getter(), b.getter()) } What do I have to add to the definition of g to make it work? porky11 I found a solution. It's not done by a where clause, but this way: fn g<T: A, U: A<A = T::A>>(a: T, b: U) { //

How to avoid circular dependencies when setting Properties?

眉间皱痕 提交于 2019-12-01 21:37:31
This is a design principle question for classes dealing with mathematical/physical equations where the user is allowed to set any parameter upon which the remaining are being calculated. In this example I would like to be able to let the frequency be set as well while avoiding circular dependencies. For example: from traits.api import HasTraits, Float, Property from scipy.constants import c, h class Photon(HasTraits): wavelength = Float # would like to do Property, but that would be circular? frequency = Property(depends_on = 'wavelength') energy = Property(depends_on = ['wavelength, frequency