Relation between ECS and Rust's trait system?

Instead of doing inheritance, Rust's type system says:

  1. type X implements traits T!, T2, ...
  2. we can do Foo for all types that implement T1 and T3

In ECS, instead of doing inheritance, we say:

  1. an entity has components C1, C2, ...
  2. we have a system Foo that runs on all entities with components C1 and C3


I am wondering if the similarity if only skin deep, or if there is a deeper connection between ECS and Rust's trait system. (cool interactions between ECS & rust's trait system welcome too)

I would say they are similar in that both favour composition over inheritance, but the approach they take is the mirror opposite. Component in an ECS are purely about data and have no behaviour, whereas traits are just about behaviour and can't hold data.

Of course I can always use systems to attach some behaviour to components, and traits can use getters/setters for data access. This line of thought reminds me of the Closures and Objects are Equivalent koan - all programming is pretty much the same, we just happen to use different terminology to describe it (objects vs closures, components vs getters and struct properties, systems vs methods, etc.).


I might be taking this analogy a step too far and thus unintentionally attacking a straw man.

  1. I agree that objects == closures in the following sense: take two languages with identical semantics except one has closure and the other has objects -- we can probably write a 1-2 page match statement / rewrite rule that translates the AST of one into the other. This might even be an exercise in SICP.

  2. I am aware that Scheme/Lisp often claims 'code is data; data is code'. I also understand that "all languages are sorta equiv as they are all turing complete"

  3. Intuitively, I think there is something else going on with regards to ECS vs traits for the following reason: traits are a language construct; ECS is often implemented as a library.

It might be possible that there exists a procedural macro that can transform arbitrary Rust trait into ECS -- but this is not obvious to me. Again, I might be reading into your analogy more than you are claiming.

And ECS is effectively just a table. You can organize virtually anything in tables, you just need some sort of column/row identity function. So the analogy you're making isn't all that surprising: at least no more than say, realizing that the Periodic table of Elements is a table, and thus you can think of the presence of ECS components and Rust Traits on data analogously to the presence of electrons in specific orbitals around atoms.

The real question is what do you suppose could be meaningfully done with the relation? If you don't care about things like performance, there are a billion interesting things you could implement. But if you do care about performance, you wouldn't tolerate the vast majority of those things.

This looks like a complicated way to claim "the relation is only skin deep"

You are welcome to post your own question, but please do not hijack my question. The 'real question', as I literally wrote in the original post, is:

You might believe, based on your "argument" above, that the relation is obviously skin deep, and thus the 'real question' lies elsewhere -- however, I do not find your argument mathematically rigorous, and although I do not yet see a deeper connection between ECS / trait systems, I can not rule one out. Thus, the 'real question' still is:

First off, I made no argument. There's no need to be rude.

Secondly, if you want mathematical rigor out of this discussion, you need to provide theorems and axioms. Waving your hands around and saying "maybe there's something deep here" is not a reliable approach to innovation. You can find similarities between any two things if you look hard enough. But that's not empowering unless you're looking for some kind of advantage, and the relation you're seeing is predicated on the conditions of that advantage. But you haven't proposed any advantage, and I'm asking you to consider looking for one.

  1. I agree that I do not have a rigorous definition. However, in mathematics, being able to rigorously define a new concept is often half the battle; it should not be surprising that I do not yet have a rigorous definition for something I am trying to understand search for.

  2. I criticized your argument, not you. I do not know why you feel necessary to make personal attacks on the way I analyze arguments:

  1. Can we play stay on topic? The question at hand is: whether similarities between ECS trait system is skin deep or if there is something deeper.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.