Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-12

An idea for a "pair" type

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

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

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph via fpc-pascal schrieb am
Mi., 16. Dez. 2020, 18:38:

Show quoted text 16 lines
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.

This can simply be done using existing language constructs, no need to introduce anything new.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
On Dec 16, 2020, at 10:50 AM, Sven Barth wrote: This can simply be done using existing language constructs, no need to introduce anything new.

That's what I thought you'd say. ;) It's basically syntactic sugar surrounding a generic record, so yes technically it is. It's just something I use so often I figure it warrants being included in the language itself. I use them in Swift, JavaScript, Python and PHP. It's very handy syntax and proven itself in the industry by 2020.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph via fpc-pascal schrieb am
Mi., 16. Dez. 2020, 19:41:

Show quoted text 15 lines
> On Dec 16, 2020, at 10:50 AM, Sven Barth wrote: > > This can simply be done using existing language constructs, no need to introduce anything new. > That's what I thought you'd say. ;) It's basically syntactic sugar surrounding a generic record, so yes technically it is. It's just something I use so often I figure it warrants being included in the language itself. I use them in Swift, JavaScript, Python and PHP. It's very handy syntax and proven itself in the industry by 2020.

No, something being used often is not sufficient cause for a language extension. A cause for a language extension is something that is not easily achieved using existing mechanisms, essentially pure syntactic sugar does not count as such.

Regards,
Sven

Reply by email