traits

How to overload class constructor within traits in PHP >= 5.4

浪子不回头ぞ 提交于 2019-12-17 15:32:52
问题 In PHP 5, I can to overload constructors (and any others methods). But if I get some code like this: class Base { public function __construct($a, $b) { echo $a+$b; } public function sayHello() { echo 'Hello '; } } trait SayWorld { public function __construct($a, $b, $c = 0) { echo (int)$c * ($a+$b); } public function sayHello($a = null) { parent::sayHello(); echo 'World!'.$a; } } class MyHelloWorld extends Base { use SayWorld; } $o = new MyHelloWorld(2, 3); $o->sayHello(1); I have an error:

Trait for numeric functionality in Rust

試著忘記壹切 提交于 2019-12-17 07:52:14
问题 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() // ... } 回答1: 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));

How do traits classes work and what do they do?

一笑奈何 提交于 2019-12-17 07:13:18
问题 I'm reading Scott Meyers' Effective C++. He is talking about traits classes, I understood that I need them to determine the type of the object during compilation time, but I can't understand his explanation about what these classes actually do? (from technical point of view) 回答1: Perhaps you’re expecting some kind of magic that makes type traits work. In that case, be disappointed – there is no magic. Type traits are manually defined for each type. For example, consider iterator_traits ,

How can I avoid a ripple effect from changing a concrete struct to generic?

回眸只為那壹抹淺笑 提交于 2019-12-17 05:14:34
问题 I have a configuration struct that looks like this: struct Conf { list: Vec<String>, } The implementation was internally populating the list member, but now I have decided that I want to delegate that task to another object. So I have: trait ListBuilder { fn build(&self, list: &mut Vec<String>); } struct Conf<T: Sized + ListBuilder> { list: Vec<String>, builder: T, } impl<T> Conf<T> where T: Sized + ListBuilder, { fn init(&mut self) { self.builder.build(&mut self.list); } } impl<T> Conf<T>

Why would I implement methods on a trait instead of as part of the trait?

牧云@^-^@ 提交于 2019-12-17 05:04:56
问题 While trying to understand the Any trait better, I saw that it has an impl block for the trait itself. I don't understand the purpose of this construct, or even if it has a specific name. I made a little experiment with both a "normal" trait method and a method defined in the impl block: trait Foo { fn foo_in_trait(&self) { println!("in foo") } } impl dyn Foo { fn foo_in_impl(&self) { println!("in impl") } } impl Foo for u8 {} fn main() { let x = Box::new(42u8) as Box<dyn Foo>; x.foo_in_trait

Why would I implement methods on a trait instead of as part of the trait?

浪子不回头ぞ 提交于 2019-12-17 05:04:08
问题 While trying to understand the Any trait better, I saw that it has an impl block for the trait itself. I don't understand the purpose of this construct, or even if it has a specific name. I made a little experiment with both a "normal" trait method and a method defined in the impl block: trait Foo { fn foo_in_trait(&self) { println!("in foo") } } impl dyn Foo { fn foo_in_impl(&self) { println!("in impl") } } impl Foo for u8 {} fn main() { let x = Box::new(42u8) as Box<dyn Foo>; x.foo_in_trait

How to clone a struct storing a boxed trait object?

南楼画角 提交于 2019-12-17 03:16:31
问题 I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box<Animal> . trait Animal { fn speak(&self); } struct Dog { name: String, } impl Dog { fn new(name: &str) -> Dog { return Dog { name: name.to_string(), }; } } impl Animal for Dog { fn speak(&self) { println!{"{}: ruff, ruff!", self.name}; } } struct AnimalHouse { animal: Box<Animal>, } fn main() { let house = AnimalHouse { animal: Box:

How to convert an Option<T> to an iterator of zero or one element?

≡放荡痞女 提交于 2019-12-14 03:54:01
问题 I'm trying to decode a digit to an integer and either get an iterator over just this digit or an empty iterator if it wasn't a digit. I tried to do it like that: let ch = '1'; ch.to_digit(10).map(once).unwrap_or(empty()) This doesn't compile. I get the following error message: error[E0308]: mismatched types --> src/lib.rs:6:41 | 6 | ch.to_digit(10).map(once).unwrap_or(empty()); | ^^^^^^^ expected struct `std::iter::Once`, found struct `std::iter::Empty` error[E0308]: mismatched types --> src

How to handle inheritance from two similar sub-classes?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 00:27:24
问题 I've used the first two videos in this series to learn about some basic OOP concepts. Lately, I primarily write in Node, so I'm working with prototypical inheritance on the front-end and back-end. However, these tutorials showcase OOP concepts with Java. Java is a strictly-typed language which utilizes classical inheritance . This question pertains to both classical and prototypical inheritance, but in different ways. This problem is a little bit difficult to put into words, so I'll use an

Scala - trait member initialization: use traits to modify class member

醉酒当歌 提交于 2019-12-13 15:15:41
问题 Probably the Title is not so clear. This is my problem. Let's say I have a trait that defines an application with a series of configuration parameters. These parameters are contained in a Map , some of them have default values. trait ConfApp { val dbName: String lazy val conf: scala.collection.mutable.Map[String, Any] = scala.collection.mutable.Map("db" -> dbName, "foo" -> "bar") } So I can create a custom application as follows: class MyApp extends ConfApp { override val dbName = "my_app_db"