Giter Site home page Giter Site logo

Comments (2)

tgolsson avatar tgolsson commented on July 21, 2024 1

Started hacking on the compiler side just to see where it slots it on and where it'll land in the final data. Progress branch here: https://github.com/tgolsson/rune/tree/ts/traits.

This is the code that I want to get to compile, ~ish, as a proof of concept.

trait MyTrait {
    fn echo(self);
    fn name(self) {
        "Derek"
    }
}

struct MyStruct { a }

impl MyTrait for MyStruct {
    fn echo(self) { MyTrait::name(self) }
}

pub fn main() {
    let m = MyStruct { a: 10 };
    m.echo()    
}

from rune.

tgolsson avatar tgolsson commented on July 21, 2024

@udoprog I started thinking about this last night from a "native" side, since I want to emulate Deref for my generic structs - I've got something like

#[derive(Any)]
struct DerefProxy<T>
where
    T: runestick::Any,
{
    inner: AtomicRefMut<'static, T>, 
}

And this wrapper (whose sole purpose is to keep the lock alive) should be Deref<Target = T> just like the inner value is. I'm not sure about exact patterns to make it ergonomic and safe -- I guess there'll need to be some form of "cook" once all traits etc have been registered to generate a dispatch table. My current working idea for surface API is that there'll be something like this:

struct TraitApi { /* todo */ }
trait InstallTrait { /* todo */ }

impl<T> InstallTrait for T where T: std::fmt::Debug {
    fn api() -> TraitApi { ... }
    
    fn install(m: &mut Module) -> ... {
         m.inst_trait("std::fmt::Debug",
             Self::api().inst_fn("fmt", |s: &T, f: &mut String| { ... })
    }
}

Where each Module::inst_trait call is one segment of a virtual table. And then e.g STRING_DEBUG would be vm.dispatch("std::fmt::Debug", "fmt", target, args) or something like that. Haven't thought too much about this from the compiler perspective though, I guess there needs to be some form of resolution in untyped code -- e.g., how'd one differentiate between mutable/non-mutable access, etc, or simply name clashes between unrelated generic names (get, set, update, fetch, run, ...). You know more about the compiler than I do so I'd love to hear your thoughts on that side.

from rune.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.