logo
down
shadow

Mixin or Trait implementation in AS3?


Mixin or Trait implementation in AS3?

By : user3862058
Date : November 22 2020, 07:01 PM
will help you Zero solutions presented on this, so I looked into a few methods. There are ECMA script style mixins by adding methods defined on other objects to the base objects prototype. But this means that the advantages of static typing are gone.
I was looking for a solution that didn't sidestep the static type system. I knew that ASMock used bytecode injection to create proxy classes. I hacked around ASMock for the past few days and came up with a possible solution implemented by creating a class with composed classes (through bytecode injection).
code :


Share : facebook icon twitter icon
Trait implementation for both a trait object and for direct implementors of the trait

Trait implementation for both a trait object and for direct implementors of the trait


By : Cop3Tx
Date : March 29 2020, 07:55 AM
around this issue Implement your trait for all Box where S implements your trait. Then you simply delegate to the existing implementation:
code :
impl<S: Solid + ?Sized> Solid for Box<S> {
    fn intersect(&self, ray: f32) -> f32 {
        (**self).intersect(ray)
    }
}
impl<'a, S: Solid + ?Sized> Solid for &'a S {
    fn intersect(&self, ray: f32) -> f32 {
        (**self).intersect(ray)
    }
}
trait Solid {
    fn intersect(&self, ray: f32) -> f32;
}

impl<S: Solid + ?Sized> Solid for Box<S> {
    fn intersect(&self, ray: f32) -> f32 {
        (**self).intersect(ray)
    }
}

impl<'a, S: Solid + ?Sized> Solid for &'a S {
    fn intersect(&self, ray: f32) -> f32 {
        (**self).intersect(ray)
    }
}

struct Group<S>(Vec<S>);

impl<S: Solid> Solid for Group<S> {
    fn intersect(&self, ray: f32) -> f32 {
        42.42
    }
}

struct Point;

impl Solid for Point {
    fn intersect(&self, ray: f32) -> f32 {
        100.
    }
}

fn main() {
    let direct = Group(vec![Point]);
    let boxed = Group(vec![Box::new(Point)]);
    let pt = Point;
    let reference = Group(vec![&pt]);

    let mixed: Group<Box<Solid>> = Group(vec![
        Box::new(direct),
        Box::new(boxed),
        Box::new(Point),
        Box::new(reference),
    ]);

    mixed.intersect(1.0);
}
Writing a generic trait implementation with IntoIterator trait, but only for immutable reference implementors

Writing a generic trait implementation with IntoIterator trait, but only for immutable reference implementors


By : Nanchao Phanomphume
Date : March 29 2020, 07:55 AM
I hope this helps . Saying that I is an IntoIterator doesn't say anything about &I, e.g. x..y is an IntoIterator (because it is an Iterator and all of them are), but &(x..y) is not.
You specifically want to bound &I, which fortunately can be done via a where clause, e.g.
code :
impl<I, T> Bar<I> for Test 
    where for<'a> &'a I: IntoIterator<Item = T>
{
    fn bar(&self, i: &I) {
        for x in i {
            println!("woo!");
        }
    }
}
Trait instantiation using another mixin trait

Trait instantiation using another mixin trait


By : sokarab
Date : March 29 2020, 07:55 AM
To fix the issue you can do From section 5.1 of the spec:
Mismatched types error when moving a method from a trait implementation to a trait definition

Mismatched types error when moving a method from a trait implementation to a trait definition


By : moonlight
Date : March 29 2020, 07:55 AM
I hope this helps you . if you move execute in Core, there is nothing in the trait definition that says that T is the same type as Self.
code :
trait Core<T> {
  fn word(&self) -> &Word<T>;
  fn hello(&self) { println!("Hello"); }
  fn execute(&self) { 
      (self.word() // this is a &Word<T>
          .action) // this is a fn(T)
          (self);  // this type is Self. T is not necessarily = Self
  }
}
trait Core: Sized {
  fn word(&self) -> &Word<Self>; // now you can't parametrize what 
                                 // Word to return. It will be Self.
  fn hello(&self) { println!("Hello"); }
  fn execute(&self) { (self.word().action)(self); } // ...and this works
}
How can I implement the From trait for all types implementing a trait but use a specific implementation for certain type

How can I implement the From trait for all types implementing a trait but use a specific implementation for certain type


By : roman andra
Date : March 29 2020, 07:55 AM
will be helpful for those in need Since &str and String both implement ToString, you can use the unstable specialization feature:
code :
#![feature(specialization)]

use std::borrow::Cow;

pub struct Luhn<'a> {
    code: Cow<'a, str>,
}

impl<'a, T: ToString> From<T> for Luhn<'a> {
    default fn from(input: T) -> Self {
//  ^^^^^^^
        Luhn {
            code: Cow::Owned(input.to_string()),
        }
    }
}

impl<'a> From<&'a str> for Luhn<'a> {
    fn from(input: &'a str) -> Self {
        Luhn {
            code: Cow::Borrowed(input),
        }
    }
}

impl<'a> From<String> for Luhn<'a> {
    fn from(input: String) -> Self {
        Luhn {
            code: Cow::Owned(input),
        }
    }
}
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk