Helper for Generic

Issue #238 closed
Toky Olivier Razanakotonarivo created an issue

Hello,

How can I do something like this in DWScript please?

  ForEach<T> = helper for Array of T
    procedure forEach(procedure(value: T));
  end;

Comments (4)

  1. Toky Olivier Razanakotonarivo reporter

    As Helper for Variant is supported, I did like this:

    MutationObserverCallback := procedure (mutationList: TQTXMutationCollection; observer: TQTXMutationObserver)
        begin
          var mutations: array of variant := mutationList;
          mutations.forEach(procedure(mutation: variant)
          begin
            var added: array of variant;
            asm @added=(@mutation).addedNodes end;
            added.forEach(procedure(elt: variant)
            begin
    
            end);
          end);
        end;
    

  2. Eric Grange repo owner

    Auto-instantiation of generics is not active yet, you have to manually instantiate them for each type you want them applied to.
    The limitation for that comes from consideration of compile time performance and not having had time yet to reverse engineer the scoping priorities Delphi uses for generics in those cases.

    That said, ForEach is a good candidate for language support, if only because it would map so directly with the JS codegen, and script-side, in most cases it could be inlined as a classic loop directly without having to resort to full-blown inlining machinery.

  3. Log in to comment