This repository has been archived on 2024-12-25. You can view files and clone it, but cannot push or open issues or pull requests.
2024-03-10 20:32:51 +03:00

29 lines
1.4 KiB
ObjectPascal
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Пример иллюстрирует использование знака "&" для явного указания шаблонного типа подпрограммы
function GetDefault<T>: T :=
default(T);
type
GenericType<T> = class
constructor := exit;
static procedure p1 := writeln(typeof(T));
end;
begin
// var o := GetDefault<byte>; //Ошибка: Встречено ';', а ожидалось выражение
// компилятор не отличает знак сравнения < от открытия угловой скобки
// и поэтому видит эту строчку как "(GetDefault < byte) > ;"
// ";" там оказывается неожиданно, потому что компилятор ожидал ещё какое то выражение для сравнения
// но, знаком "&" можно экранировать "<"
// то есть переключить значение "<" со знака сравнения на открывающуюся скобочку:
var o := GetDefault&<byte>;
// в случае с шаблонными типами - обычно "&" не нужен
var a := new GenericType<word>;
// однако для вызова статичного метода - его так же надо поставить
GenericType&<real>.p1;
end.