On 10/06/2012 11:14 AM, Eddy Cizeron wrote:

Hello,

I'm new to Rust, and I have some questions concerning some aspects of
the language. From what I understand until now, rust has no dynamic
  linkage for methods as it would be the case in any standard OOP
language. This is surely an intentionaly different philosophy from the
current trend among other recent programming languages. But why not
after all. I like the idea. So I find myself skeptical when I read the
chapters of the documentation about OOP-like concepts like classes or
interfaces.

Hi!

I can understand how you would have this perception, as most Rust code favors using traits as Haskell-style type classes, with static dispatch (it is more efficient). OO-style dynamic dispatch is an important use case though, and Rust supports it, but the implementation is not as mature.

On Rust's master branch you can do things like this:

    impl @Circle: Drawable { fn draw() { ... } }

    impl @Rectangle: Drawable { fn draw() { ... } }

    let c: @Circle = @new_circle();
    let r: @Rectangle = @new_rectangle();

    // Cast box things that implement Drawable to boxed Drawables
    draw_all(~[c as @Drawable, r as @Drawable]);

This gives you dynamic dispatch on any Drawable through a managed (@) vtable (fwiw I don't know if this is the recommended syntax - this is still in development).

I recommend giving the 0.4 tutorial's section on [generics] a read. It does a much better job than the 0.3 tutorial at explaining how it all works. I hope that it will relieve some of your concerns.

[generics]: http://dl.rust-lang.org/doc/tutorial.html#generics

Additionally your concern about resources and classes not needing to be interrelated is spot on, and that issue will be resolved. In 0.4 still structs are the only type that contain destructors, but the intent is to make destructors just an implementation of `trait Drop` (or `Finalize` maybe).

-Brian


_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to