traits

Trait not implemented for (thing that implements trait)

随声附和 提交于 2019-12-13 10:19:20
问题 So, Rust is trying to tell me a fib, I think, but maybe I'm just out of my mind... fn get_random<T, R>(range: Range<T>, rng: &mut R) -> T where T: SampleRange + PartialOrd, R: Rng { range.ind_sample(&mut rng) } The where clause there should indicate that R definitely implements Rng, otherwise... Well, come on, right? But when I try to compile this, it swears up and down that rng does not implement rand::Rng. What on earth? rustc 1.0.0-nightly (cfea8ec41 2015-03-10) (built 2015-03-11) (in case

Is it possible to figure out the parameter type and return type of a lambda?

坚强是说给别人听的谎言 提交于 2019-12-13 06:06:47
问题 Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how? Basically, I want lambda_traits which can be used in following ways: auto lambda = [](int i) { return long(i*10); }; lambda_traits<decltype(lambda)>::param_type i; //i should be int lambda_traits<decltype(lambda)>::return_type l; //l should be long The motivation behind is that I want to use lambda_traits in a function template which accepts a lambda as argument, and I need to know it's parameter

How to implement Display on a trait object where the types already implement Display

China☆狼群 提交于 2019-12-13 03:47:40
问题 I have some code which returns a trait object of type MyTrait so that it can return one of several different structs. I would like to implement the Display trait for the trait object so that I can print the object, with the details delegated to the various structs as they each need their own custom formatters. I can achieve this by including a formatting method as part of the MyTrait definition, and then implementing Display for MyTrait and delegating - like this: trait MyTrait { fn is_even(

encapsulation for mixin's members in Scala

你说的曾经没有我的故事 提交于 2019-12-12 15:46:31
问题 Traits in Scala can be used as both mixins and interfaces. It leads to some inconsistence - if I want to close some method inside trait, I just can't do that: object Library { protected trait A { def a: Int = 5 } trait B extends A { private override def a: Int = super.a } //I want to close `a` memeber for all traits extending B; it's still possible to open it in some another trait `C extends A`, or even `Z extends B with C` } // Exiting paste mode, now interpreting. <console>:10: error:

Restriction of access to function [duplicate]

别等时光非礼了梦想. 提交于 2019-12-12 14:33:04
问题 This question already has answers here : std::enable_if to conditionally compile a member function (6 answers) Closed 6 years ago . I have a generic class with a function that I want to restrict to instances of floating point types only, at compile time. As shown in the example below: template <typename T> class ClassName { // instance variables, etc.. void some_method() { // do stuff, but only for floating point types } } How do I make the compiler reject the usage of some_method for

Box<T> to &T in Rust

风格不统一 提交于 2019-12-12 13:17:22
问题 How do I call a function that expects a trait object if I have a Box<T> instead? In other words: trait T { ... } fn func(t: &T) { ... } fn some_other_func() { b: Box<T>; // Provided // These work, but is there a better way? func( &*b ); // 1 func( Borrow::borrow(&b) ); // 2 } Both 1 and 2 seem wrong. Am I missing something obvious? 回答1: &*foo is called a "reborrow", and is idiomatic. 来源: https://stackoverflow.com/questions/32084849/boxt-to-t-in-rust

What happens when you create a Seq object with Seq(1,2,3)?

落爺英雄遲暮 提交于 2019-12-12 13:14:29
问题 What exactly happens when you evaluate expression: Seq(1,2,3) ? I am new to Scala and I am now a bit confused about various collection types. Seq is a trait, right? So when you call it like that Seq(1,2,3) it must be some kind of a companion object? Or not? Is it some kind of a class that extends Seq? And most importantly what is the type of the returned value? Is it Seq and if yes, why is not explicitly the extension class instead? Also in REPL I see that the contents of the evaluated

Why doesn't String implement From<&String>?

我怕爱的太早我们不能终老 提交于 2019-12-12 13:06:55
问题 Background I know that in Rust people prefer &str rather than &String . But in some case we were only given &String . One example is when you call std::iter::Iterator::peekable . The return value is a Peekable<I> object that wraps the original iterator into it and gives you one extra method peek . The point here is that peek only gives you a reference to the iterator item. So if you have an iterator that contains String s, you only have &String in this case. Of cause, you can easily use as

How do I organize data by common traits?

不想你离开。 提交于 2019-12-12 12:34:45
问题 I'm having trouble cataloging data in a way that allows me to reference data by its common descriptors or traits. I'm well aware of inheritance, traits (the programming concept), and interfaces, but none of those seems to be the right answer to my problem. I'm writing a program in JavaScript that has potentially many different items or objects. Let's say I have a datatype of WoodenShortSword and I want to express that it has the traits of being Flammable and Weapon and OneHanded . Then, I

Meaning of super in stacked traits depends on call site?

耗尽温柔 提交于 2019-12-12 12:23:31
问题 I can't come up with a very good description of this in words, so, please take a look at this example: trait Base { def foo = "Base" } trait One extends Base { override def foo = "One <: " + super.foo } trait Two extends Base { override def foo = "Two <: " + super.foo } new Base with One with Two {} foo This prints: Two <: One <: Base , which is what I expect. Now, I am trying to add another level, so that overriding traits would not have to call super explicitly. Like this: trait Base { def