traits

Inheritance and code reuse in stackable traits

若如初见. 提交于 2019-11-27 07:55:09
问题 In this simplified experiment, I want to be able to quickly build a class with stackable traits that can report on what traits were used to build it. This reminds me strongly of the decorator pattern, but I'd prefer to have this implemented at compile time rather than at runtime. Working Example with Redundant Code class TraitTest { def report(d: Int) : Unit = { println(s"At depth $d, we've reached the end of our recursion") } } trait Moo extends TraitTest { private def sound = "Moo" override

How to implement a trait for a parameterized trait

南楼画角 提交于 2019-11-27 07:50:55
问题 I have a design issue, when using something like : trait MyTrait<K: OtherTrait> { ... } impl<K: OtherTrait, M: MyTrait<K>> AnyTrait for M { ... } I cannot implement trait for this trait due to E207 error ("the type parameter K is not constrained by the impl trait, self type, or predicates"). Finding no way to get rid of this error, I apply this not-so-good-looking workaround (verbose and struct with no intrinsic value): use std::fmt; use std::marker::PhantomData; pub trait MyTrait<K: fmt:

Why can't I add a blanket impl on a trait with a type parameter?

流过昼夜 提交于 2019-11-27 07:46:05
问题 Consider these two traits: pub trait Foo { fn new(arg: u32) -> Self; } pub trait Bar<P>: Foo { fn with_parameter(arg: u32, parameter: P) -> Self; } I'd like to add the blanket impl: impl<T: Bar<P>, P: Default> Foo for T { fn new(arg: u32) -> Self { Self::with_parameter(arg, P::default()) } } But I get the compiler error: error[E0207]: the type parameter `P` is not constrained by the impl trait, self type, or predicates --> src/lib.rs:9:17 | 9 | impl<T: Bar<P>, P: Default> Foo for T { | ^

Traits as a return value from a function [duplicate]

混江龙づ霸主 提交于 2019-11-27 07:41:42
问题 This question already has an answer here: How do I return an instance of a trait from a method? 3 answers I have two enums, NormalColour and BoldColour , both of which implement the Colour trait. They contain Blue , BoldGreen , and so on. I'd like to return values of both of these types from the same function, treating them as though they're just a Colour value, calling the paint function on the result, but I can't find a way to coerce the Rust complier into doing this for me. I'd like to be

Specify `Fn` trait bound on struct definition without fixing one of the `Fn` parameters

流过昼夜 提交于 2019-11-27 07:40:43
问题 I have a struct that contains a function object: struct Foo<F> { func: F, } I want to add an Fn trait bound to the struct definition. The problem is: I do care about the first parameter (it has to be i32 ), but not the second one. What I actually want to write is something like this: struct Foo<F> where ∃ P so that F: Fn(i32, P), { func: F, } So in English: the type F has to be a function that takes two parameters, the first of which is an i32 (and the second one can be anything). The syntax

can a scala self type enforce a case class type

做~自己de王妃 提交于 2019-11-27 07:07:08
问题 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

Trait to check if some specialization of template class is base class of specific class

岁酱吖の 提交于 2019-11-27 05:58:01
问题 There is std::is_base_of in modern STL. It allow us to determine whether the second parameter is derived from first parameter or if they are the same classes both or, otherwise, to determine is there no such relation between them. Is it possible to determine whether the one class is derived from some concrete template class without distinction of which concrete actual parameters involved to its specialization? Say, we have; template< typename ...types > struct B {}; And template< typename ..

How do I use floating point number literals when using generic types?

一世执手 提交于 2019-11-27 05:39:09
Regular float literals do not work: extern crate num_traits; use num_traits::float::Float; fn scale_float<T: Float>(x: T) -> T { x * 0.54 } fn main() { let a: f64 = scale_float(1.23); } error[E0308]: mismatched types --> src/main.rs:6:9 | 6 | x * 0.54 | ^^^^ expected type parameter, found floating-point variable | = note: expected type `T` found type `{float}` Use the FromPrimitive trait : use num_traits::{cast::FromPrimitive, float::Float}; fn scale_float<T: Float + FromPrimitive>(x: T) -> T { x * T::from_f64(0.54).unwrap() } Or the standard library From / Into traits fn scale_float<T>(x: T)

Generics Error: expected type parameter, found struct

风流意气都作罢 提交于 2019-11-27 05:36:32
I started a new project, where I want to be as modular as possible, by that I mean that I would like to be able to replace some parts with others in the future. This seems to be a perfect use for traits , at the moment I have this code: mod parser; mod renderer; mod renderers; use parser::MarkParser; use renderer::MarkRenderer; struct Rustmark <P: MarkParser, R: MarkRenderer> { parser: P, renderer: R, } impl <P: MarkParser, R: MarkRenderer> Rustmark <P, R> { fn new() -> Rustmark <P, R> { Rustmark { parser: parser::DefaultParser::new(), renderer: renderers::HTMLRenderer::new(), } } fn render(

Trait for numeric functionality in Rust

[亡魂溺海] 提交于 2019-11-27 05:35:48
Is there any trait that specifies some numeric functionality? I'd like to use it for bounding a generic type, like this hypothetical HasSQRT : fn some_generic_function<T>(input: &T) where T: HasSQRT { // ... input.sqrt() // ... } evilone You can use num or num-traits crates and bound your generic function type with num::Float , num::Integer or whatever relevant trait: extern crate num; use num::Float; fn main() { let f1: f32 = 2.0; let f2: f64 = 3.0; let i1: i32 = 3; println!("{:?}", sqrt(f1)); println!("{:?}", sqrt(f2)); println!("{:?}", sqrt(i1)); // error } fn sqrt<T: Float>(input: T) -> T