traits

can a scala self type enforce a case class type

痴心易碎 提交于 2019-11-28 12:39:17
Would there be any way in scala, to define a trait's self type to be a case class, as in "any case class"? I would like a self type to be able to use the .copy method of a case class, enforcing that its self type is some case class not a regular class. Structural types, I think, won't help, as they require a signature comprising specific arguments (I can probably not structural-type generically for any case class ). Please forgo the "if you need that you must be doing something wrong", as I've already moved on but my api design - will have been slicker if the above were possible. I am also

How do I combine multiple functions using Diesel into one through abstraction?

∥☆過路亽.° 提交于 2019-11-28 11:46:13
问题 I have the following two functions: pub fn get_most_recent_eth_entry(conn: &SqliteConnection) -> Result<i32, Error> { let res = types::ethereum::table .order(types::ethereum::time.desc()) .limit(1) .load::<types::ETHRecord>(&*conn); match res { Ok(x) => { if x.len() > 0 { Ok(x.get(0).unwrap().time) } else { Ok(0) } } Err(err) => Err(format_err!("Error here! {:?}", err)), } } pub fn get_most_recent_btc_entry(conn: &SqliteConnection) -> Result<i32, Error> { let res = types::bitcoin::table

What is a concise way to inform the compiler of the specifc type with multiple implementations of a generic trait?

☆樱花仙子☆ 提交于 2019-11-28 11:08:05
问题 I've come across an odd type inference problem that has me scratching my head a bit. I'm implementing a generic trait on a struct for multiple types. I started with &str : struct Bar<'a> { baz: &'a str, } trait Foo<T> { fn foo(&self) -> T; } impl<'a> Foo<&'a str> for Bar<'a> { fn foo(&self) -> &'a str { self.baz } } #[cfg(test)] mod tests { use super::*; #[test] fn test_str_a() { let bar = Bar { baz: "asd" }; assert_eq!("asd", bar.foo()); } } This works — the problem comes when I add another

Dynamicly creating class with trait binding

天大地大妈咪最大 提交于 2019-11-28 11:00:53
问题 I want to make use of traits in my project, and for multiple inheriance I want to use traits. So I created some traits to use eg: tItem_Epic, tItem_Weapon, Item_Driver When I create new class for Sword, I thought I could use eval to create class: <?php function new_item_class($type) { eval('class Item_'.ucfirst($type).' extends Item_Driver { use tItem_Epic, tItem_Weapon; }'); } ?> This is an example. There are some more parameters that change the course of eval (like: item quality, etc.).

Is it possible to automatically implement a trait for any tuple that is made up of types that all implement the trait?

拈花ヽ惹草 提交于 2019-11-28 10:45:39
问题 Suppose that I have a trait Happy {} I can implement Happy for whatever struct I might want, for example: struct Dog; struct Cat; struct Alligator; impl Happy for Dog {} impl Happy for Cat {} impl Happy for Alligator {} Now, I would like to automatically impl my Happy trait for whatever tuple is made up of types that all implement the Happy trait. Intuitively, a tuple of all happy is happy as well. Is it possible to do such a thing? For example, I can trivially extend the implementation of

How to deduce class type from method type in C++ templates?

喜欢而已 提交于 2019-11-28 09:54:12
问题 In templates as shown below, I would like the call Run(&Base::foo) succeed without the need to name the Base type twice (as is done in the compiling Run<Base>(&Base::foo) call). Can I have that? Possibly without adding a ton of Boost headers? With the provided code, I get an error of: prog.cpp:26: error: no matching function for call to ‘Run(bool (Base::*)())’ (you can fiddle with the snippet at http://ideone.com/8NZkq): #include <iostream> class Base { public: bool foo() { return true; } };

Implementing a “cautious” take_while using Peekable

我与影子孤独终老i 提交于 2019-11-28 08:00:09
问题 I'd like to use Peekable as the basis for a new cautious_take_while operation that acts like take_while from IteratorExt but without consuming the first failed item. (There's a side question of whether this is a good idea, and whether there are better ways to accomplish this goal in Rust -- I'd be happy for hints in that direction, but mostly I'm trying to understand where my code is breaking). The API I'm trying to enable is basically: let mut chars = "abcdefg.".chars().peekable(); let abc :

Why must the associated type be specified in a collection of references to types implementing a trait?

心不动则不痛 提交于 2019-11-28 07:52:54
问题 Here is an offending example: // Some traits trait Behaviour { type Sub: SubBehaviour; } trait SubBehaviour {} // Some implementations of these traits struct A; impl Behaviour for A { type Sub = B; } struct B; impl SubBehaviour for B {} // Struct that holds a collection of these traits. struct Example<'a> { behaviours: Vec<&'a Behaviour>, } impl<'a> Example<'a> { fn add_behaviour<T: Behaviour>(&mut self, b: &'a T) { self.behaviours.push(b); } } fn main() { let b = A; let mut e = Example {

What is the difference between <T: Trait> Box<T> and &Trait / Box<Trait>?

送分小仙女□ 提交于 2019-11-28 07:18:00
问题 When writing code with traits you can put the trait in a trait bound: use std::fmt::Debug; fn myfunction1<T: Debug>(v: Box<T>) { println!("{:?}", v); } fn myfunction2<T: Debug>(v: &T) { println!("{:?}", v); } fn main() { myfunction1(Box::new(5)); myfunction2(&5); } Or directly in a Box or reference type: use std::fmt::Debug; fn myfunction3(v: Box<Debug>) { println!("{:?}", v); } fn myfunction4(v: &Debug) { println!("{:?}", v); } fn main() { myfunction3(Box::new(5)); myfunction4(&5); } These

Difference between trait inheritance and self type annotation

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:43:53
In Scala, I've seen the constructs trait T extends S and trait T { this: S => used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why would you use one over the other? I'd use self-types for dependency-management: This trait requires another trait to be mixed in. And I'd use inheritance to refine another trait or interface. Just as an example: trait FooService trait FooRemoting { this : FooService => } trait FooPersistence { this : FooService => } object Services extends FooService with