traits

How do I implement the Add trait for a reference to a struct?

自作多情 提交于 2019-11-26 14:40:09
I made a two element Vector struct and I want to overload the + operator. I made all my functions and methods take references, rather than values, and I want the + operator to work the same way. impl Add for Vector { fn add(&self, other: &Vector) -> Vector { Vector { x: self.x + other.x, y: self.y + other.y, } } } Depending on which variation I try, I either get lifetime problems or type mismatches. Specifically, the &self argument seems to not get treated as the right type. I have seen examples with template arguments on impl as well as Add , but they just result in different errors. I found

How would you implement a “trait” design-pattern in C#?

橙三吉。 提交于 2019-11-26 13:00:34
问题 I know the feature doesn\'t exist in C#, but PHP recently added a feature called Traits which I thought was a bit silly at first until I started thinking about it. Say I have a base class called Client . Client has a single property called Name . Now I\'m developing a re-usable application that will be used by many different customers. All customers agree that a client should have a name, hence it being in the base-class. Now Customer A comes along and says he also need to track the client\'s

Linearization order in Scala

我的未来我决定 提交于 2019-11-26 12:23:32
问题 I have difficulties in understanding the linearization order in Scala when working with traits: class A { def foo() = \"A\" } trait B extends A { override def foo() = \"B\" + super.foo() } trait C extends B { override def foo() = \"C\" + super.foo() } trait D extends A { override def foo() = \"D\" + super.foo() } object LinearizationPlayground { def main(args: Array[String]) { var d = new A with D with C with B; println(d.foo) // CBDA???? } } It prints CBDA but I can\'t figure out why. How is

Mixing in a trait dynamically

ⅰ亾dé卋堺 提交于 2019-11-26 12:21:54
问题 Having a trait trait Persisted { def id: Long } how do I implement a method that accepts an instance of any case class and returns its copy with the trait mixed in? The signature of the method looks like: def toPersisted[T](instance: T, id: Long): T with Persisted 回答1: This can be done with macros (that are officially a part of Scala since 2.10.0-M3). Here's a gist example of what you are looking for. 1) My macro generates a local class that inherits from the provided case class and Persisted

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

南楼画角 提交于 2019-11-26 11:39:22
问题 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}` 回答1: Use the FromPrimitive trait: use num_traits::{cast::FromPrimitive, float::Float}; fn scale_float<T: Float + FromPrimitive>(x: T)

Generics Error: expected type parameter, found struct

假如想象 提交于 2019-11-26 11:38: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> {

Automatically implement traits of enclosed type for Rust newtypes (tuple structs with one field)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:23:26
问题 In Rust, tuple structs with only one field can be created like the following: struct Centimeters(i32); I want to do basic arithmetic with Centimeters without extracting their \"inner\" values every time with pattern matching, and without implementing the Add , Sub , ... traits and overloading operators. What I want to do is: let a = Centimeters(100); let b = Centimeters(200); assert_eq!(a + a, b); 回答1: is there a way to do it without extracting their "inner" values every time with pattern

Traits in PHP – any real world examples/best practices? [closed]

大憨熊 提交于 2019-11-26 10:05:53
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Traits have been one of the biggest additions for PHP 5.4. I know the syntax and understand the idea behind traits, like horizontal

Traits in javascript [closed]

别来无恙 提交于 2019-11-26 09:29:20
问题 How can I implement traits in javascript ? 回答1: function Trait (methods) { this.traits = [methods]; }; Trait.prototype = { constructor: Trait , uses: function (trait) { this.traits = this.traits.concat (trait.traits); return this; } , useBy: function (obj) { for (var i = 0; i < this.traits.length; ++i) { var methods = this.traits [i]; for (var prop in methods) { if (methods.hasOwnProperty (prop)) { obj [prop] = obj [prop] || methods [prop]; } } } } }; Trait.unimplemented = function (obj,

Traits vs. interfaces

▼魔方 西西 提交于 2019-11-26 08:39:08
问题 I\'ve been trying to study up on PHP lately, and I find myself getting hung up on traits. I understand the concept of horizontal code reuse and not wanting to necessarily inherit from an abstract class. What I don\'t understand is: What is the crucial difference between using traits versus interfaces? I\'ve tried searching for a decent blog post or article explaining when to use one or the other, but the examples I\'ve found so far seem so similar as to be identical. 回答1: An interface defines