traits

How can I implement Deref for a struct that holds an Rc<Refcell<Trait>>?

核能气质少年 提交于 2020-03-02 09:26:31
问题 My goal is to delegate method calls against my struct to a Trait's methods, where the Trait object is inside an Rc of RefCell . I tried to follow the advice from this question: How can I obtain an &A reference from a Rc<RefCell<A>>? I get a compile error. use std::rc::Rc; use std::cell::RefCell; use std::fmt::*; use std::ops::Deref; pub struct ShyObject { pub association: Rc<RefCell<dyn Display>> } impl Deref for ShyObject { type Target = dyn Display; fn deref<'a>(&'a self) -> &(dyn Display +

How to check if a class uses a trait in PHP?

☆樱花仙子☆ 提交于 2020-02-21 10:09:16
问题 Notice that a trait may use other traits, so the class may not be using that trait directly. And also the class may be inherited from a parent class who is the one uses the trait. Is this a question that can be solved within several lines or I would have to do some loops? 回答1: The class_uses() function will return an array containing the names of all traits used by that class, and will work by passing it a class name or an instance.... however, you'd need to "recurse" through the inheritance

Understanding Php Traits Scope

时光怂恿深爱的人放手 提交于 2020-02-06 07:56:52
问题 I have The following files 1) A Class called Helper class Helper { public static function a(){ // } } 2) A Trait called SpecialTrait Trait SpecialTrait { public function b(){ Helper::a(); } } 3) A Controller Class Extending Controller called UserController use path\to\Helper; use path\to\SpecialTrait class UserController extends Controller { use SpecialTrait; public function c(){ $this->b(); } } When the program runs and function c() is called which is meant to call a trait function b() which

Groovy traits in java classes

淺唱寂寞╮ 提交于 2020-02-06 00:49:11
问题 I am working on an existing java codebase but have convinced the team to use cross compilation so new development can be done in groovy, while still using the old codebase. Best of both worlds, low risk, lots of benefits, etc. I have a problem i am trying to solve that is perfectly handled by Groovy's trait feature, but it has to work with the existing java classes or new ones for the dev's that still want to write in java. doing class duck implements FlyingAbility { in java throws an error

Groovy traits in java classes

末鹿安然 提交于 2020-02-06 00:48:36
问题 I am working on an existing java codebase but have convinced the team to use cross compilation so new development can be done in groovy, while still using the old codebase. Best of both worlds, low risk, lots of benefits, etc. I have a problem i am trying to solve that is perfectly handled by Groovy's trait feature, but it has to work with the existing java classes or new ones for the dev's that still want to write in java. doing class duck implements FlyingAbility { in java throws an error

How to define lifetimes on a Fn trait bound returning references?

巧了我就是萌 提交于 2020-02-05 04:34:29
问题 I'm trying to create a struct with a field, generic over F where F implements something like: Fn(&mut Compiler, &[Token]) -> &Token . The only issue is, I'm not sure how I define lifetimes on the Fn trait which satisfy the constraint that the returned &Token references data in the &[Token] slice supplied as an argument. Everything I've tried has thrown cryptic errors thus far. Here is an MVCE which demonstrates the code (without any lifetimes): struct Compiler; #[derive(Debug)] struct Token

HRTB: Concrete type bounded by a trait containing lifetime type parameter vs bounded by only a lifetime type parameter

一世执手 提交于 2020-02-04 03:58:26
问题 This is actually an offshoot of this SO question. Consider the following code: trait Trait<'b> { fn func(&'b self) {} } struct Struct {} impl<'s> Trait<'s> for Struct {} fn test<'s, T:'s>(t: T) where T: Trait<'s>, { t.func(); } It fails, as the compiler sees that t lives for less than the 's and 's is set by the caller (i.e longer than the stack-frame of the func ) and traits are invariant over type parameters. However, if I introduce HRTB (Higher Rank Trait Bounds) here, the code compiles:

Why PHP Trait can't implement interfaces?

半世苍凉 提交于 2020-01-30 14:07:21
问题 I'm wondering why PHP Trait (PHP 5.4) cannot implement interfaces. Update from user1460043's answer => ...cannot require class which uses it to implement a specific interface I understand that it could be obvious, because people could think that if a Class A is using a Trait T which is implementing an interface I , than the Class A should be implementing the interface I undirectly (and this is not true because Class A could rename trait methods). In my case, my trait is calling methods from

lifetime bound on associated type is rejected although it seems valid

三世轮回 提交于 2020-01-30 05:44:08
问题 I have a piece of code that does not compile and that can be reduced to this snippet: use std::error::Error; use std::convert::TryFrom; // A trait that provides methods for parsing data into a type T. pub trait Deserializable<T> { // some methods } pub struct MyBuffer<'a> { inner: &'a [u8] } impl<'a, T> Deserializable<T> for MyBuffer<'a> where T: TryFrom<&'a [u8]>, <T as TryFrom<&'a [u8]>>::Error: Error + Sync + Send + 'static { // some methods to implement } fn main() {} The compiler rejects

Complex trait requirements on struct

独自空忆成欢 提交于 2020-01-24 15:05:46
问题 I have a fairly complex trait set up and I'm having trouble lining the pieces up. Right now it looks roughly like this: /// Trait for models which can be gradient-optimized. pub trait Optimizable { type Data; type Target; // The contract // } /// Trait for optimization algorithms. pub trait OptimAlgorithm<M : Optimizable> { // The contract // } Now I want to be able to allow a struct implementing OptimAlgorithm to be a field in a struct implementing Optimizable . This would look something