traits

Mixins for ES6 classes, transpiled with babel

天涯浪子 提交于 2019-11-26 08:19:54
问题 According to various sources (2ality, esdiscuss) one should be able to add mixins to classes: EDIT discovered that class methods are not enumerable so that cannot work. Edited the code below, but still no joy class CartoonCharacter { constructor(author) { this.author = author; } drawnBy() { console.log(\"drawn by\", this.author); } } // THIS CANNOT WORK // class methods are not enumerable // class Human { // haveFun() { // console.log(\"drinking beer\"); // } // } let Human = Object.create({}

Java 8 default methods as traits : safe?

亡梦爱人 提交于 2019-11-26 07:55:40
问题 Is it a safe practice to use default methods as a poor\'s man version of traits in Java 8? Some claim it may make pandas sad if you use them just for the sake of it, because it\'s cool, but that\'s not my intention. It is also often reminded that default methods were introduced to support API evolution and backward compatibility, which is true, but this does not make it wrong or twisted to use them as traits per se. I have the following practical use case in mind: public interface Loggable {

What is a sealed trait?

落花浮王杯 提交于 2019-11-26 07:51:22
问题 Sealed classes are described in \'Programming in Scala\', but sealed traits are not. Where can I find more information about a sealed trait? I would like to know, if a sealed trait is the same as a sealed class? Or, if not, what are the differences? When is it a good idea to use a sealed trait (and when not)? 回答1: A sealed trait can be extended only in the same file as its declaration. They are often used to provide an alternative to enums . Since they can be only extended in a single file,

The trait cannot be made into an object

纵饮孤独 提交于 2019-11-26 07:47:13
问题 I have the following code: extern crate futures; // 0.1.24 use futures::Future; use std::io; struct Context; pub trait MyTrait { fn receive(context: Context) -> Future<Item = (), Error = io::Error>; } pub struct MyStruct { my_trait: MyTrait, } When I try to compile it I get the error message: error[E0038]: the trait `MyTrait` cannot be made into an object --> src/lib.rs:13:5 | 13 | my_trait: MyTrait, | ^^^^^^^^^^^^^^^^^ the trait `MyTrait` cannot be made into an object | = note: method

When to use val or def in Scala traits?

為{幸葍}努か 提交于 2019-11-26 07:25:11
问题 I was going through the effective scala slides and it mentions on slide 10 to never use val in a trait for abstract members and use def instead. The slide does not mention in detail why using abstract val in a trait is an anti-pattern. I would appreciate it if someone can explain best practice around using val vs def in a trait for abstract methods 回答1: A def can be implemented by either of a def , a val , a lazy val or an object . So it's the most abstract form of defining a member. Since

Is it possible to access struct fields from within a trait?

为君一笑 提交于 2019-11-26 06:49:04
问题 Traits are used to group some functions to be implemented from a struct, but is it possible to access struct fields from within the trait? I could imagine declaring fields inside the trait so that the fields are abstracted as well. I haven\'t found such a syntax; is there any other solution? Otherwise, it wouldn\'t be possible to have non-static methods using a trait, would it? I know object oriented programming from C# and I\'m playing around with Rust, trying to adapt the OOP functionality

When should I not implement a trait for references to implementors of that trait?

谁说胖子不能爱 提交于 2019-11-26 06:46:57
问题 If I have a trait, and a function that accepts a generic type constrained to that type, everything works fine. If I try to pass in a reference to that type, I get a compilation error. trait Trait { fn hello(&self) -> u32; } struct Struct(u32); impl Trait for Struct { fn hello(&self) -> u32 { self.0 } } fn runner<T: Trait>(t: T) { println!(\"{}\", t.hello()) } fn main() { let s = Struct(42); // Works runner(s); // Doesn\'t work runner(&s); } error[E0277]: the trait bound `&Struct: Trait` is

How to override trait function and call it from the overridden function?

半腔热情 提交于 2019-11-26 06:12:12
问题 Scenario: trait A { function calc($v) { return $v+1; } } class MyClass { use A; function calc($v) { $v++; return A::calc($v); } } print (new MyClass())->calc(2); // should print 4 This code doesn\'t work, and I cannot find a way to call a trait function like it was inherited. I tried calling self::calc($v) , static::calc($v) , parent::calc($v) , A::calc($v) and the following: trait A { function calc($v) { return $v+1; } } class MyClass { use A { calc as traitcalc; } function calc($v) { $v++;

Find out whether a C++ object is callable

你离开我真会死。 提交于 2019-11-26 05:28:22
问题 Is it possible to write a type trait, say is_callable<T> which tells if an object has an operator() defined? It is easy if the arguments to the call operator are known in advance, but not in the general case. I want the trait to return true if and only if there is at least one overloaded call operator defined. This question is related and has a good answer, but it doesn\'t work on all types (only on int -convertible types). Also, std::is_function works, but only on proper C++ functions, not

What makes something a “trait object”?

☆樱花仙子☆ 提交于 2019-11-26 04:46:52
问题 Recent Rust changes have made \"trait objects\" more prominent to me, but I only have a nebulous grasp of what actually makes something into a trait object. One change in particular is the upcoming change to allow trait objects to forward trait implementations to the inner type. Given a trait Foo , I\'m pretty sure that Box<Foo> is a trait object. Is &Foo also a trait object? What about other smart-pointer things like Rc or Arc ? How could I make my own type that would count as a trait object