[fpc-pascal] Interface syntax: Is possible don't use specialize in mode objfpc?

Maciej Izak hnb.code at gmail.com
Thu Jul 16 22:17:04 CEST 2015


2015-07-16 17:23 GMT+02:00 Sven Barth <pascaldragon at googlemail.com>:

> Then let me tell you that generic methods will arrive in mode ObjFPC (and
> thus its syntax) first, because the Delphi syntax is a PITA to parse/handle.
>
> (in fact generic methods are already working in mode ObjFPC in my local
> repo, I just want to get the Delphi ones working a little bit before
> commiting to trunk)
>

@Sven, @Michael

I am using generics all the time for many things.

IMO using "generic/specialize" syntax because it is more simple to
implement in compiler is very strange motivation. Using specialize keyword
maybe is good in pure theory but is non practical (generic keyword in type
declaration is not so bad).

You can torture me (!!!) but I will not use so many unnecessary words for
simple things.
By using this complicated syntax you will kill all functional/generic
approach.

Advice "if you like shorter syntax then go to other language" is no
solution for me. I like some modern versions of Pascal like presented in
Oxygen and till Delphi XE2 (! many things after XE2 is pure evil).

I am working on smart pointers/objects (some evolution/for the purposes of
Generics.Collections). Syntax with generic/specialize is not "smart" for my
branch:

=============== CODE BEGIN ===============
{$MODE SPARTA}
var
  X: strong TDictionary<weak TList<Integer>, string>;

{ sugar syntax for
var
  X: SmartObj<TDictionary<WeakObj<TList<Integer>>, string>>;  }
===============  CODE END  ===============

so probably in ObjFPC, code above will become:

=============== CODE BEGIN ===============
{$MODE OBJFPC}
type
  TList_Integer = specialize TList<Integer>;
  WeakObj_TList_Integer = specialize WeakObj<TList_Integer>;
  TDictionary_WeakObj_TList_Integer_String = specialize
TDictionary<WeakObj_TList_Integer, string>;
  SmartObj_TDictionary_WeakObj_TList_Integer_String = specialize
SmartObj<TDictionary_WeakObj_TList_Integer_String>;

var
  X: SmartObj_TDictionary_WeakObj_TList_Integer_String;
===============  CODE END  ===============

Another simple example. Maybe not a good example to follow, but it shows
the problems:

===============FPC CODE BEGIN===============
type
  generic ISomeInterface<T> = interface
    function Invoke(AValue: T): Boolean;
  end;

  // arghhhhhh... O____o generics types that are in half generic...
  TList_Integer = specialize TList<Integer>;
  TDictionary_TInteger_List_String = specialize TDictionary<TIntegerList,
string>;
  TArray_TList_Integer = specialize TArray<TListInteger>;
  ISomeInterface_TArray_TList_Integer = specialize
ISomeInterface<TArrayTListInteger>;
  TEqualityComparer_TList_Integer = specialize
TEqualityComparer<TList_Integer>;
  TFunc_TArray_TList_Integer_RESULT_Boolean = specialize TFunc<
TArray_TList_Integer, Boolean>;
  TComparer_TList_Integer = specialize TComparer<TList_Integer>;

var
  d: TDictionary_TInteger_List_String;
  e: TArray_TList_Integer;
  c: ISomeInterface_TArray_TList_Integer;
  l: TInteger_List;
begin
  // you are killing future usage of closures
  d := TDictionary_TInteger_List_String.Create(
      TEqualityComparer_TList_Integer.Construct(
        function(constref ALeft, ARight: TInteger_List): Boolean
        begin
          // ...
        end,

        function(constref AValue: TInteger_List): UInt32
        begin
          // ...
        end
      )
    );

  // another example with closures
  TFunc_TArray_TList_Integer_RESULT_Boolean(c)
    := function(AValue: TArray_TList_Integer): Boolean
       var
         LIndex: Integer;
       begin
         TArray.specialize BinarySearch<TList_Integer>(
           AValue, SomeList, LIndex,
           TComparer_TList_Integer.Construct(
             function(constref Left, Right: TList_Integer): Integer
             begin
               // ...
             end
           )
         )
       end;

  c(l);
  e := d.Keys.ToArray();
  // you are killing simple things
  TArray.specialize Sort<TList_Integer>(e,
TComparer_TList_Integer.Construct(
      function(constref Left, Right: TList_Integer): Integer
      begin
        // ...
      end)
    );
=============== FPC CODE END ===============


===============DELPHI CODE BEGIN===============
type
  ISomeInterface<T> = interface
    function Invoke(AValue: T): Boolean;
  end;

var
  d: TDictionary<TList<Integer>, string>;
  e: TArray<TList<Integer>>;
  c: ISomeInterface<TArray<TList<Integer>>>;
  l: TList<Integer>;
begin
  d := TDictionary<TList<Integer>, string>.Create(
      TEqualityComparer<TList<Integer>>.Construct(
        function(const ALeft, ARight: TList<Integer>): Boolean
        begin

        end,

        function(const AValue: TList<Integer>): Integer
        begin

        end
      )
    );

  TFunc<TArray<TList<Integer>>, Boolean>(c)
    := function(AValue: TArray<TList<Integer>>): Boolean
       var
         LIndex: Integer;
       begin
         TArray.BinarySearch<TList<Integer>>(
           AValue, l, LIndex,
           TComparer<TList<Integer>>.Construct(
             function(const Left, Right: TList<Integer>): Integer
             begin
               //...
             end
           )
         )
       end;

  c(l);
  e := d.Keys.ToArray();
  TArray.Sort<TList<Integer>>(e, TComparer<TList<Integer>>.Construct(
      function(const Left, Right: TList<Integer>): Integer
      begin
        //...
      end)
    );
=============== DELPHI CODE END ===============


Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150716/4ac4d8c7/attachment.html>


More information about the fpc-pascal mailing list