traits

“The parameter type `C` may not live long enough”, when it doesn't need to

随声附和 提交于 2019-12-24 01:16:14
问题 I'm writing very basic AI system in Rust. It's main components are: Action s, which can be implemented by library user, for specific use, Generic Context , which is passed to all actions, and only needs to live during the action execution, ActionsContainer , which "globally" stores all possible actions, System , which chooses the correct action and runs it. There are many systems, one for each agent. However, they share the same set of behaviours, so they all reference a common

How to invoke a concrete Scala trait method from Java?

坚强是说给别人听的谎言 提交于 2019-12-24 00:53:37
问题 I have a Java / Scala mix Maven project. I need to reuse a Saddle method make that is concretely defined as part of a trait called Index . The method is defined here if that helps in any way. I have tried calling that method from java using Index.make or Index$class.make but in both cases I get the error: cannot find symbol compilation error. Is there a way to call a concrete Trait method from Java? 回答1: A trait is similar to a Java interface in a sense that it is not a concrete constructor.

Unsatisfied trait bound involving associated type

左心房为你撑大大i 提交于 2019-12-23 21:42:38
问题 The code pub trait Q<S> { fn f(); } pub trait A { type I; type F: Q<Self::I>; } // this works (1) // // pub struct S<T> // where // T: A // { // unsatisfied trait bound (2) pub struct S<T> where T: A<I = bool>, { t: T, } fails to compile: error[E0277]: the trait bound `<T as A>::F: Q<bool>` is not satisfied --> src/main.rs:18:1 | 18 | / pub struct S<T> 19 | | where 20 | | T: A<I = bool>, 21 | | { 22 | | t: T, 23 | | } | |_^ the trait `Q<bool>` is not implemented for `<T as A>::F` | = help:

What's the correct method for using Traits and Namespaces for CakePHP 2?

北城以北 提交于 2019-12-23 20:00:46
问题 I'm using CakePHP 2.4.5 and PHP 5.5, and would like to use a trait. I have a trait in Utility/VariablesTrait.php called VariablesTrait . To take advantage of namespaces, I've given it a namespace of App\Utility\VariablesTrait , since Utility\VariablesTrait seems a bit too global, and the former would work better with CakePHP 3. In my class that I want to use it in, I have the use App\Utility\VariablesTrait; statement in the class. For backup, I also have a App::uses('VariablesTrait', 'Utility

Diamond Issue with Groovy traits

邮差的信 提交于 2019-12-23 19:37:54
问题 In many blog about Groovy Traits it's mentioned that it will solve the Diamond Problem. But the concept is not clear to me that how traits will solve the Diamond Problem. Can any one explain please. 回答1: The diamond problem is a problem when you have multiple inheritance and two or more super classes define one or more functions with the same signature. With groovy traits, the behaviour is well-defined. By default, the last implementation is chosen. trait A { String name() { "A" } } trait B {

Implementing a generic incrementable trait in Rust

不羁的心 提交于 2019-12-23 16:27:36
问题 I'm trying to understand how to implement a generic trait in Rust. While I've seen a number of examples, the examples are too tied to a specific use (e.g. genomic mutators) for me to be able to understand at this point in my Rust development. Instead, here's a simple example based on something fairly universal--incrementing: trait Incrementable { fn post_inc(&mut self) -> Self; fn post_inc_by(&mut self, n: usize) -> Self; } impl Incrementable for usize { fn post_inc(&mut self) -> Self { let

Is it a compiler limitation that reports “conflicting implementations of trait” even though it's impossible for a type to implement a trait?

瘦欲@ 提交于 2019-12-23 10:04:57
问题 When attempting to answer this question, I wrote this code: trait MyTrait { type A; type B; } trait WithFoo: MyTrait { fn foo(a: Self::A) -> Self::B; } impl<T, U: MyTrait<A = T, B = T>> WithFoo for U { fn foo(a: T) -> T { a } } struct S1; impl MyTrait for S1 { type A = u32; type B = f32; } impl WithFoo for S1 { fn foo<T>(a: Self::A) -> Self::B { a as f32 } } struct S2; impl MyTrait for S2 { type A = u32; type B = u32; } fn main() { S1::foo(42); S2::foo(42); } playground This fails to compile

Accessing a class template parameter type inside a member function with a lambda fails

流过昼夜 提交于 2019-12-23 09:16:30
问题 I have a class template with a member function that has a lambda which wants to use a class template parameter type. It fails to compile inside the lambda but succeeds, as anticipated, outside the lambda. struct wcout_reporter { static void report(const std::wstring& output) { std::wcout << output << std::endl; } }; template <typename reporter = wcout_reporter> class agency { public: void report_all() { reporter::report(L"dummy"); // Compiles. std::for_each(reports_.begin(), reports_.end(),

How can I define a function with a parameter that can be multiple kinds of trait objects?

坚强是说给别人听的谎言 提交于 2019-12-23 09:16:18
问题 I'm trying to define a function that will take a reference as a parameter, and call a generic method on the referenced object, passing in a concrete value. I need a way of requiring that the generic type of the parameter passed to my function is a trait of the concrete type that the function will use it with. I can't seem to work out how to do this. A minimal example of the sort of thing I'm trying to achieve: trait Vehicle {} trait Floating {} struct Boat; impl Vehicle for Boat {} impl

Is there any way to work around an unused type parameter?

非 Y 不嫁゛ 提交于 2019-12-23 08:06:19
问题 Code: trait Trait<T> {} struct Struct<U>; impl<T, U: Trait<T>> Struct<U> {} Error: error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates --> src/main.rs:5:6 | 5 | impl<T, U: Trait<T>> Struct<U> {} | ^ unconstrained type parameter It seems that RFC 447 prohibits this pattern; is there any way to work around this? I think it could be solved by changing T to an associated type, but that would prevent me from doing multidispatch. 回答1: A type parameter