traits

Why can't a class extend traits with method of the same signature?

房东的猫 提交于 2019-11-29 20:27:22
Why do I get the error below? How to workaround it? I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to order. scala> trait A { def hi = println("A") } defined trait A scala> trait B { def hi = println("B") } defined trait B scala> class C extends B with A <console>:6: error: error overriding method hi in trait B of type => Unit; method hi in trait A of type => Unit needs `override' modifier class C extends B with A scala> trait A { override def hi =

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

ぐ巨炮叔叔 提交于 2019-11-29 18:12:30
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 .order(types::bitcoin::time.desc()) .limit(1) .load::<types::BTCRecord>(&*conn); match res { Ok(x) => { if x

ES 6 Classes - Mixins

自古美人都是妖i 提交于 2019-11-29 18:03:45
I'm coming up with View (HTML markup) and Utility (JavaScript - behavior) architecture and creating atomic classes for composing views and utilities using ES6 Class. There will be a need that multiple utility classes can be composed/mixed into a single view class. How can ES6 Class API provide a way to mix-in class(es) into another/main class. I've looked at Object.assign but that is for objects and not at class-level. JavaScript classes right now and hopefully also in future only can be extended from each other but can not be mixed into one another. If at all, then most probably Lightweight

Rust generics: Expected <T> found <Foo>

半腔热情 提交于 2019-11-29 17:56:53
I'm trying to use generics but I don't master that topic well enough and I get this error: error: mismatched types: expected `book::mdbook::MDBook<R>`, found `book::mdbook::MDBook<renderer::html_handlebars::HtmlHandlebars>` (expected type parameter, found struct `renderer::html_handlebars::HtmlHandlebars`) [E0308] This is the relevant code pub struct MDBook<R> where R: Renderer { title: String, author: String, config: BookConfig, pub content: Vec<BookItem>, renderer: R, } impl<R> MDBook<R> where R: Renderer { pub fn new(path: &PathBuf) -> Self { MDBook { title: String::from(""), author: String

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

我只是一个虾纸丫 提交于 2019-11-29 16:58:46
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 implementation for the u8 type: struct Bar<'a> { baz: &'a str, } trait Foo<T> { fn foo(&self) -> T; }

How to use Traits - Laravel 5.2

久未见 提交于 2019-11-29 16:02:05
问题 I'm new to Traits, but I have a lot of code that is repeating in my functions, and I want to use Traits to make the code less messy. I have made a Traits directory in my Http directory with a Trait called BrandsTrait.php . And all it does is call on all Brands. But when I try to call BrandsTrait in my Products Controller, like this: use App\Http\Traits\BrandsTrait; class ProductsController extends Controller { use BrandsTrait; public function addProduct() { //$brands = Brand::all(); $brands =

Type definition with a trait: Differences of specifying an explicit lifetime bound?

送分小仙女□ 提交于 2019-11-29 15:42:49
问题 I'm having issues understanding the lifetime bound requirements when I use a trait in a type definition. For instance: trait Kind { /* ... */ } type CollectionOfKind<'a> = Vec<&'a Kind>; // => error: explicit lifetime bound required The requirement for lifetime bounds has already been discussed for the case of a trait within a struct (Answer 1, Answer 2). At first, I had problems to apply the approach of "adding the lifetime" here at all, since this does not work: type CollectionOfKind<'a> =

Javascript Traits Pattern Resources

纵饮孤独 提交于 2019-11-29 15:17:58
问题 Could anyone recommend good resources for using traits in javascript? After some searching I mainly find articles about libraries that provide traits functionality, but I was curious about best practices on how to implement traits without a library. I came across this post on SO, are there any other approaches? Traits in javascript Any real world examples would be welcome as well. Thanks. 回答1: I would suggest something simple, along the lines of: Let traits be defined as standard JavaScript

How to Box a trait that has associated types?

只谈情不闲聊 提交于 2019-11-29 15:01:08
I'm very new to Rust so I may have terminology confused. I want to use the hashes crates to do some hashing and I want to dynamically pick which algorithm (sha256, sha512, etc.) to use at runtime. I'd like to write something like this: let hasher = match "one of the algorithms" { "sha256" => Box::new(Sha256::new()) as Box<Digest>, "sha512" => Box::new(Sha512::new()) as Box<Digest> // etc... }; I sort of get that that doesn't work because the associated types required by Digest aren't specified. If I attempt to fill them in: "sha256" => Box::new(Sha256::new()) as Box<Digest<<OutputSize = U32,

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

好久不见. 提交于 2019-11-29 14:22:40
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 { behaviours: Vec::new(), }; e.add_behaviour(&b); } Playground I am getting: error[E0191]: the value of the