Hi all.

Hi all.
How can I cast TArray to IEnumerable?

I need this to avoid duplicate code:

class function TCheck.Every(Source: IEnumerable; Lambda: TPredicateRef): Boolean;
var
  X: T;
begin
  Result := True;
  for X in Source do
    if not Lambda(X) then
      Exit(False);
end;

class function TCheck.Every(Source: TArray; Lambda: TPredicateRef): Boolean;
var
  X: T;
begin
  Result := True;
  for X in Source do
    if not Lambda(X) then
      Exit(False);
end;

Comments

  1. You cannot cast TArray to IEnumerable. What you can do is create a helper method of some description that will create a newly minted IEnumerable when passed a TArray. I would expect that frameworks like Spring have such things ready made.

    ReplyDelete
  2. Yup, TCollections.Query (1.1) or TEnumerable.Query (develop/1.2) wraps the passed TArray into an IEnumerable.

    Also Spring4D already contains the IEnumerable.All method that does exactly what your Every method does.

    ReplyDelete
  3. Nonamed Pravdin This is generics via templates a-la C++ !  :-)

    ReplyDelete
  4. +David Manual inclusion is a giant step from templates

    ReplyDelete
  5. David Heffernan yes, it allows much more granularity. I like to think about $include as the same as $region.

    ReplyDelete
  6. David Berneda I don't think so. Template specialization in C++ allows huge granularity and flexibility. Template meta programming, being Turing complete. Try that with $INCLUDE.

    ReplyDelete
  7. I could find myself liking loading code off into include files if the editor would blend such code in nicely, but jumping through several files that just contain tiny snippets of code without context is just f***ing horrible and only a cure to a symptom.

    ReplyDelete

Post a Comment