pas2js Generics

From Free Pascal wiki
Jump to navigationJump to search

Overview

Working

  • Simple generic record with simple members name:T

ToDos

  • Delphi type overload, e.g. type TArr = array of word; TArr<T> = array of T; TArr<T,S> = record i:T; j:S end;
  • cascaded specialize: TBird<word>.TWing<byte>
  • nested specialize: TBird<TWing<byte>>
  • anonymous type in specialize: TBird<array of word>
  • generic class:
    • default classname "TTest<System.Longint>"
    • pas2js classname "TTest$G1" and overloads
    • descendants cannot refer to type parameters of ancestors
    • members can refer to itself without parameters:
    • enumtype inside generic is not propagated
    • nested generic: allowed by Delphi, not allowed by FPC
    • method implementation
      • Delphi: must repeat type params, can omit constraints
      • FPC: must not repeat type params
    • forward class (FPC does support it yet)
  • generic record:
  • generic interface:
  • generic array:
  • generic static array: Note: delphi wiki says "no static arrays", but 10.3 compiles it, see http://docwiki.embarcadero.com/RADStudio/Rio/en/Declaring_Generics
  • generic procedural type:
  • generic function (aka parametrized method):
    • Delphi does not allow global generic functions, only generic methods
    • methods: virtual, message, constructor and destructors cannot have type parameters
  • constraints:
    • "class", "record", "constructor" (class+default constructor, can be combined with class)
    • multiple interface types
    • one class type (Delphi: not TObject)
    • forward class must repeat constraints
  • specialize method:
    • explicitly specifying Fly<word>(aWord)
    • automatic inferring types Fly(aWord), $modeswitch implicitfunctionspecialization MyProc(1) calls MyProc<T>(a: T)
  • specialized proc types can assign normal proc
  • Statements:
    • inline specialization
      • Delphi: TFoo<Integer>.Create
      • FPC: specialize TFoo<Integer>.Create
    • T is TFoo<Integer>
    • T as TFoo<Integer>
    • TFoo<Integer>(expr)
    • "obj is T": allowed with constraint "class" or class type
    • typecast T(): allowed with constraint "class" or class type
    • typeinfo(T)
    • using implementation function in generic function:
      • Delphi: Error: Method of parameterized type declared in interface section must not use local symbol '%s'
      • FPC: Error: Global Generic template references static symtable
    • anInt:=GenericVar
      • Delphi: always Error: Incompatible types: 'Integer' and 'T'
      • FPC: allowed for valid combinations -> operators are checked on specialization, Note: because of implementation cross uses, check must be delayed until used unit implementation is complete
    • call
      • Delphi: must fit the constraint. For example without constraint it only fits untyped args.
      • FPC: if only one function in scope preselect it, overloads are selected by constraints alone. Later checked by specialization.