← Back to context

Comment by rezonant

5 years ago

In the other branch I touched on this- iterating children still being a likely use case after all, you have the option of exposing the original or making a copy which could have perf impacts.

But honestly best to not preoptimize, I would probably do

private _children : Entity[];

get children() { return this._children.slice(); }

And reconsider the potential mutation risk later if the profiler says it matters.

It can be, though there are some interesting philosophical issues there.

The example that I always keep coming back to is Smalltalk, which is the only language I know of that represents pure object-oriented programming. Similar to how, for the longest time, Haskell was more-or-less the only purely functional language. Anyway, in Smalltalk you generally would not do that. You'd tell the object to iterate its own children, and give it some block (Smalltalk equivalent of an anonymous function) that tells it what to do with them.

Looping over a data structure from the outside is, if you want to get really zealous about it, is more of a procedural and/or functional way of doing things.

  • Indeed! It's the visitor pattern and since we're talking about a tree that would probably be useful here.