Lemmings Forums

Lemmings Boards => Help & Guides => Topic started by: Simon on February 08, 2025, 07:12:16 PM

Title: Delphi, understanding the NL/CE codebase
Post by: Simon on February 08, 2025, 07:12:16 PM
Here are questions from Tuesday, 2025-02-04 (https://www.lemmingsforums.net/index.php?msg=105516) 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
Title: Re: Delphi, understanding the NL/CE codebase
Post by: namida on February 08, 2025, 08:25:29 PM
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.
Title: Re: Delphi, understanding the NL/CE codebase
Post by: Simon on February 08, 2025, 09:47:27 PM
Thanks for the quick reply! The Delphi reference site will be useful.

-- Simon