Delphi, understanding the NL/CE codebase

Started by Simon, February 08, 2025, 07:12:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Simon

Here are questions from Tuesday, 2025-02-04 when I looked with WillLem at the NL-CE source.

Downcasting in Delphi happens with as, e.g., myDerived := myBase as Derived. This is the equivalent of Java's instanceof or C++'s dynamic_cast<Derived*>(myBase), where I get null if the runtime type isn't Derived. In OO design, it's hackish to force usercode to downcast. Thus:

Does Delphi allow to type-parametrize my own types? Or: What is the closest Delphi equivalent to C++ templates/Java generics? If we have two lists of replay entries (one for assignments, one for RR changes), I want one to be a list of Assignment and the other a list of RrChange, not two lists of Base (the base class of both Assignment and RrChange). Then my callers don't have to downcast.

What is the closest Delphi equivalent to std::span (a slice, a pointer-with-length)? E.g., my object contains the array [0, 1, 2, 3, 4, 5], I would like to return the view [2, 3, 4] to my caller without making a copy of the entries.

I expected the search function (find the assignment for a given physics update) in the list class, not in the replay manager class. The search works purely with that list and it's a natural operation for that list.

The search has at least one near-duplicate.

When your list (of assignments, of ...) is always ordered, you can find elements by binary search instead of by linear search. But this doesn't matter when you have 30 elements in the list and you search it only a few times per second. And this (5 to 100 assignments and RR changes) is the extent of most Lemmings solutions. Don't prioritize rewriting the searches to binary search.

-- Simon

namida

Not sure about span, though surely it must exist.

Generics are definitely a thing in modern (or even just modernish) versions of Delphi: https://docwiki.embarcadero.com/RADStudio/Athens/en/Overview_of_Generics
I'm pretty sure NL already makes use of generics in a few places.
My projects
2D Lemmings: NeoLemmix (engine) | Lemmings Plus Series (level packs) | Doomsday Lemmings (level pack)
3D Lemmings: Loap (engine) | L3DEdit (level / graphics editor) | L3DUtils (replay / etc utility) | Lemmings Plus 3D (level pack)
Non-Lemmings: Commander Keen: Galaxy Reimagined (a Commander Keen fangame)

Simon

Thanks for the quick reply! The Delphi reference site will be useful.

-- Simon