[fpc-pascal] An idea for a "pair" type

Ryan Joseph genericptr at gmail.com
Wed Dec 16 18:38:12 CET 2020


Just for fun I wanted to see what people thought about a potential "pair" type added to the compiler. Dictionaries have proven themselves to be a fundamental type we use often and they are appearing as built-in syntax in many languages, especially scripting languages that allow dynamic object construction.

So my idea was a potential "pair of ..." type added to the compiler which accepts pair literals in the format of "key: value". The underlying structure would be a record like "_Pair".

The primary reason for this is so we can construct dictionaries easier but they could also be used for other reasons, although I'm not sure that would be sensible. 

Seems like low hanging fruit to me but I'm curious what other people think.

=====================


type
  $TIntegerPair<T> = record
    key: ShortString; // or another type? a bare identifier maybe instead of a string?
    value: T;
  end;

TIntegerPair = pair of Integer; // defines a hidden record $TIntegerPair<Integer>

operator := (right: array of TIntegerPair): TDictionary;
begin
  // ...make the dictionary...
end;

procedure DoThis(pair: TIntegerPair);

var
  options: TDictionary;
  pair: pair of TObject;
begin

  // a pair literal
  // not a sensible use but something that could be done
  pair := 'myObject': TObject.Create;

  // array of pairs
  options := [
    'A': 1, 
    'B': 2, 
    'C': 3
  ];

  // passing a pair as a parameter.
  // again, not a sensible use but something that could be done
  DoThis('param': 1000);
end.


Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list