traits

Unit testing helper or non-interface traits in Scala

六月ゝ 毕业季﹏ 提交于 2019-11-29 11:52:22
问题 This question is about dealing with testing of classes which mix in non-interface traits, that is traits containing some functionality. When testing, the class functionality should be isolated from the functionality provided by the mix-in trait (which is supposedly tested separately). I have a simple Crawler class, which depends on a HttpConnection and a HttpHelpers collection of utility functions. Let's focus on the HttpHelpers now. In Java , HttpHelpers would possibly be a utility class,

Overriding Doctrine Trait Properties

倖福魔咒の 提交于 2019-11-29 11:05:02
问题 I know you can override a trait method by declaring it in your class, I was curious if was possible to over ride a trait Property the same way. Is this safe to do? Its not in the Documentation so I am hesitant to implement this. From the Documentation An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods. http://php.net/manual/en/language

How does the mechanism behind the creation of boxed traits work?

这一生的挚爱 提交于 2019-11-29 10:57:49
I'm having trouble understanding how values of boxed traits come into existence. Consider the following code: trait Fooer { fn foo(&self); } impl Fooer for i32 { fn foo(&self) { println!("Fooer on i32!"); } } fn main() { let a = Box::new(32); // works, creates a Box<i32> let b = Box::<i32>::new(32); // works, creates a Box<i32> let c = Box::<Fooer>::new(32); // doesn't work let d: Box<Fooer> = Box::new(32); // works, creates a Box<Fooer> let e: Box<Fooer> = Box::<i32>::new(32); // works, creates a Box<Fooer> } Obviously, variant a and b work, trivially. However, variant c does not, probably

Cannot use moved BufReader after for loop with bufreader.lines()

半世苍凉 提交于 2019-11-29 10:52:52
I'm trying to read some lines from a file, skipping the first few and printing the rest, but I keep getting errors about used value after move: use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; use std::path::Path; fn skip_and_print_file(skip: &usize, path: &Path) { let mut skip: usize = *skip; if let Ok(file) = File::open(path) { let mut buffer = BufReader::new(file); for (index, line) in buffer.lines().enumerate() { if index >= skip { break; } } print_to_stdout(&mut buffer); } } fn print_to_stdout(mut input: &mut Read) { let mut stdout = io::stdout(); io::copy(&mut input,

How can I implement Rust's Copy trait?

爷,独闯天下 提交于 2019-11-29 09:12:37
I am trying to initialise an array of structs in Rust: enum Direction { North, East, South, West, } struct RoadPoint { direction: Direction, index: i32, } // Initialise the array, but failed. let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; When I try to compile, the compiler complains that the Copy trait is not implemented: error[E0277]: the trait bound `main::RoadPoint: std::marker::Copy` is not satisfied --> src/main.rs:15:16 | 15 | let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std:

is it possible to add traits to a class in PHP in runtime?

对着背影说爱祢 提交于 2019-11-29 09:12:22
Simple question, is it possible to dynamically add traits to a php class in runtime without using eval? As Glavic said, you can't without using eval() or reflection hacks (and I'm not even sure about that). But it's very unlikely you really need to. You can achieve a lot with dynamic class composition (composing a class with some functionality you want into another class). That's simply a matter of putting a reference to the class with the desired functionality into a variable in the hosting class. class SomeClassWithNeededFunctionality {} class

How to call super method when overriding a method through a trait

旧城冷巷雨未停 提交于 2019-11-29 06:06:22
问题 It would appear that it is possible to change the implementation of a method on a class with a trait such as follows: trait Abstract { self: Result => override def userRepr = "abstract" } abstract class Result { def userRepr: String = "wtv" } case class ValDefResult(name: String) extends Result { override def userRepr = name } val a = new ValDefResult("asd") with Abstract a.userRepr Live code is available here: http://www.scalakata.com/52534e2fe4b0b1a1c4daa436 But now I would like to call the

How do you return an Iterator in Scala?

大兔子大兔子 提交于 2019-11-29 05:50:48
问题 What must I do in order to be able to return an Iterator from a method/class ? How would one add that trait to a class? 回答1: You can extend Iterator, which will require that you implement the next and hasNext methods: class MyAnswer extends Iterator[Int] { def hasNext = true def next = 42 } But, you will get more flexibility if you extend Iterable, which requires you implement elements (or iterator in 2.8): class MyAnswer extends Iterable[Int] { def iterator = new Iterator[Int] { def hasNext

How does curly braces following trait instantiation work?

只愿长相守 提交于 2019-11-29 05:39:42
I find some confusing use of trait in some unittesting code, such as: trait MyTrait { val t1 = ... //some expression val t2 = ... //some expression } And then instantiate the trait using new and meanwhile some expressions wrapped by curly braces followed the instantiation. test("it is a test") { new MyTrait { // do something with t1 and t2 } } I am confused by this strange syntax. My question is: why use follow trait instantiation by curly braces? what is the purpose of trait instantiation in this case and other cases might also be helpful? Steve Buzzard You are not instantiating the traits:

Define variable b of the same type as variable a

你离开我真会死。 提交于 2019-11-29 05:28:40
Is it possible to declare a variable var_b of the same type as another variable, var_a ? For example: template <class T> void foo(T t) { auto var_a = bar(t); //make var_b of the same type as var_a } F_1 bar(T_1 t) { } F_2 bar(T_2 t) { } Sure, use decltype : auto var_a = bar(t); decltype(var_a) b; You can add cv-qualifiers and references to decltype specifiers as if it were any other type: const decltype(var_a)* b; decltype(var_a) var_b; And a Lorem Ipsum to reach the required minimum of 30 characters per answer. Despite the nice answer of @TartanLlama, this is another way one can use decltype