lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2019-08-02 2:47 a.m., Egor Skriptunoff wrote:
On Fri, Aug 2, 2019 at 12:19 AM Soni "They/Them" L. wrote:

    > On 2019-08-01 6:05 p.m., Egor Skriptunoff wrote:
    >> Could you provide an example to show benefits of traits?
    >> What kinds of tasks traits solve better than conventional
    >> 'objects-and-methods' approach?
    >>
    >
    > I recommend reading
    https://en.wikipedia.org/wiki/Entity_component_system

    (Well, besides reading that, I recommend actually checking out
    Rust and
    using it for a while. You'll see what I mean.)


Could you just show a simple example from real-life programming where using traits is better than not using them? Such example should be understandable by any Lua programmer (without Rust knowledge). If you can't provide such example then why Lua users might want to use Cratera?

Let's say you have a trait Foo, with perhaps some methods (or all methods) depending on the object also being a Bar.

With inheritance, your Foo would be a Bar, probably, which makes it an all-or-nothing situation.

Take a look at e.g. https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.rev

Additionally, names don't matter. You can have both Foo and Bar have a reverse method and it works fine. (this is also the case in Rust)