traits

In Scala; should I use the App trait?

主宰稳场 提交于 2019-11-27 00:53:03
问题 I've just started learning Scala and many of the tutorials that I'm following are using a combination of different representations for a main method. Aside from the familiar main method; there's also the use of traits App or Application . It appears that Application has been deprecated and is not recommended, but I can't find any information that explains much beyond this about each of these ways to define an entry point. So, I'm wondering if someone could explain to me: How do the traits App

Why is “abstract override” required not “override” alone in subtrait?

痴心易碎 提交于 2019-11-27 00:43:27
问题 I read the section of Programming in Scala where abstract override is introduced, but I'm still confused by what exactly is signified by the joining of these modifiers. The snippet of code in which these modifiers is used is pasted below: trait Doubling extends IntQueue { abstract override def put(x: Int) { super.put(2 * x) } } In particular, I am confused by the purpose of abstract in this case, and why we cannot achieve the expected results simply with the override keyword. If we did not

Mixing in a trait dynamically

a 夏天 提交于 2019-11-27 00:05:44
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 Eugene Burmako 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, much like new T with Persisted would do. Then it caches its argument (to prevent multiple

What expressions are allowed as the array length N in [_; N]?

♀尐吖头ヾ 提交于 2019-11-26 23:38:54
问题 Please consider the following minimal example in Rust: const FOOBAR: usize = 3; trait Foo { const BAR: usize; } struct Fubar(); impl Foo for Fubar { const BAR: usize = 3; } struct Baz<T>(T); trait Qux { fn print_bar(); } impl<T: Foo> Qux for Baz<T> { fn print_bar() { println!("bar: {}", T::BAR); // works println!("{:?}", [T::BAR; 3]); // works println!("{:?}", [1; FOOBAR]); // works println!("{:?}", [1; T::BAR]); // this gives an error } } fn main() { Baz::<Fubar>::print_bar(); } The compiler

Can I have a static borrowed reference to a trait object?

馋奶兔 提交于 2019-11-26 23:36:09
问题 Is there a way for me to obtain a static borrowed reference to a struct's implementation of a trait: trait Trait {} struct Example; impl Trait for Example {} This works fine: static instance1: Example = Example; This also works fine: static instance2: &'static Example = &Example; But this doesn't work: static instance3: &'static Trait = &Example as &'static Trait; It fails thus: error[E0277]: the trait bound `Trait + 'static: std::marker::Sync` is not satisfied in `&'static Trait + 'static` -

Is it possible to use `impl Trait` as a function's return type in a trait definition?

血红的双手。 提交于 2019-11-26 23:34:28
问题 Is it at all possible to define functions inside of traits as having impl Trait return types? I want to create a trait that can be implemented by multiple structs so that the new() functions of all of them returns an object that they can all be used in the same way without having to write code specific to each one. trait A { fn new() -> impl A; } However, I get the following error: error[E0562]: `impl Trait` not allowed outside of function and inherent method return types --> src/lib.rs:2:17

Implement fmt::Display for Vec<T>

a 夏天 提交于 2019-11-26 23:31:13
问题 I want to implement the fmt::Display for a nested struct commonly used in my code. // The root structure pub struct WhisperFile<'a> { pub path: &'a str, pub handle: RefCell<File>, pub header: Header } pub struct Header{ pub metadata: metadata::Metadata, pub archive_infos: Vec<archive_info::ArchiveInfo> } pub struct Metadata { // SNIP } pub struct ArchiveInfo { // SNIP } As you can see, this is a non-trivial tree of data. The archive_infos property on Header can be quite long when presented as

Mixins for ES6 classes, transpiled with babel

早过忘川 提交于 2019-11-26 22:30:38
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({}, { haveFun: { enumerable: true, value: function () { console.log("drinking beer"); } } }); class

Can I avoid eager ambiguity resolution for trait implementations with generics?

ぃ、小莉子 提交于 2019-11-26 22:08:25
问题 Consider the following Rust code [playground]: use std::collections::HashMap; use std::hash::Hash; trait Foo<K> { const FOO: i32; } impl<K, K_, V> Foo<HashMap<K_, V>> for HashMap<K, V> where K: Hash + Eq + Into<K_>, { const FOO: i32 = 1; } impl<K, V, V_> Foo<HashMap<K, V_>> for HashMap<K, V> where K: Hash + Eq, V: Into<V_>, { const FOO: i32 = 2; } fn main() {} (The const is not relevant, I'd like the code to compile with fn s too). It fails to compile with the error: error[E0119]: conflicting

Compostions and mixins in JS

陌路散爱 提交于 2019-11-26 21:58:49
问题 I got some trouble with compostions and mixin . For examples, lets imagine that we have a AHero and Hero1 object. All heroes can move, so AHero.move() is a thing. And now, at a moment of the dev, I want to add the possibility to stun and beStunned. So I make a new object that look like this ( called stunTrait) : { stunned : false, stun(), beStun, } And I decide to give to possiblity to heroes to stun and beStunned. So I do a mixin : class AHero extends stunTrait(superclass). Now I want that a