@Fibonacci: when it comes to user-friendly generic syntax, it would be ideal to be able to omit the
generic and
specialize keywords, as well as avoid having to declare a separate data type for generic class constructors. In other words, it would be great if we could use the same syntax for generics by default as we do in the
{$MODE DELPHI} mode.
Below does not compile in
OBJFPC mode, compile in
DELPHI mode and should in the dialect you develop:
type
TFoo<T> = class
end;
var
Foo: TFoo<Integer>;
begin
Foo := TFoo<Integer>.Create();
end.
type
TFoo<T> = class
end;
begin
with TFoo<Char>.Create() do
Free();
end.
As for the implicit specialization of generic function calls, I don’t have a strong opinion. On the one hand, the shorter syntax requires less typing, but the compiler will have more work determining how to handle the given parameters based on their types. Here, the problem is identical to that with overloaded functions and can be just as annoying for the user as compilation errors caused by the compiler’s inability to determine which of the overloaded function versions to use. On the other hand, the requirement to specify data types in angle brackets requires writing a bit more code, but it provides clarity and makes the compiler’s job easier, since it doesn’t have to determine the types on its own (through inference). Here, I would lean toward explicitly specifying types, but of course without the
specialize keyword.
Additionally, when it comes to generic functions, it would be highly desirable to require syntactic consistency between the declaration and definition of such functions. In other words, the header of a generic function should be identical in both its declaration (the module’s
interface section) and its definition (the
implementation section). The only thing to watch out for is
forward declarations of such functions, because the language allows for a shorthand notation for forward function definitions, i.e., without specifying the list of arguments (which is absurd).
In fact, there are more of these absurd syntactic monstrosities in Pascal, because you can also omit empty parentheses in parameterless routines (declarations, definitions, calls), as well as the semicolon after the last statement in a given code block. It would be good if the dialect you're working on required the mandatory use of empty parentheses and semicolons, so as to eliminate those syntactic nonsenses. You can consider this as another suggestion for consideration and implementation.
I want to implement this. Any concrete syntax in mind?
In previous posts, I provided examples of syntax for both fixed-size arrays and dynamic arrays. This syntax is consistent with that of modern Pascal but is not as verbose, resembling the syntax used in all of today’s most popular programming languages. I will provide the examples again:
var
Foo: Integer[8]; // 1D, fixed size, 8 elements, implicit indexes 0..7
Foo: Integer[0 .. 7]; // 1D, fixed size, 8 elements, explicit indexes
var
Foo: Integer[8, 8]; // 2D, fixed size, 8x8 elements, implicit indexes 0..7,0..7
Foo: Integer[8, 1 .. 8]; // 2D, fixed size, 8x8 elements, explicit indexes 0..7,1..8
Foo: Integer[1 .. 8, 1 .. 8]; // 2D, fixed size, 8x8 elements, explicit indexes 1..8,1..8
var
Foo: Integer[]; // 1D, dynamic size, instead of "Foo: array of Integer"
Foo: Integer[,]; // 2D, dynamic size for both dimensions, instead of "Foo: array of array of Integer"
Foo: Integer[8,]; // 2D, dynamic size for 2nd dimension, instead of "Foo: array [0 .. 7] of array of Integer"
procedure Foo(ABar: const[]); // instead of "ABar: array of const"