pascalabcnet/CodeExamples/TypeclassTranslation.pas

66 lines
1.2 KiB
ObjectPascal
Raw Normal View History

2018-04-10 20:19:28 +03:00
type
ConceptAttribute = class(System.Attribute)
end;
IsConceptParameterAttribute = class(System.Attribute)
end;
ExplicitModelAttribute = class(System.Attribute)
end;
2018-04-13 18:25:29 +03:00
(*
2018-04-10 20:19:28 +03:00
[Concept] SumTC<T> = abstract class
public
constructor;
begin
end;
function sum(v1, v2: T): T; abstract;
2018-04-13 18:25:29 +03:00
end;*)
SumTC[T] = typeclass
function sum(v1, v2: T): T;
2018-04-10 20:19:28 +03:00
end;
2018-04-17 19:07:59 +03:00
(*
2018-04-10 20:19:28 +03:00
SumTC_Integer = class(SumTC<integer>)
public
constructor();
begin
end;
function sum(v1, v2: Integer): Integer; override;
begin
Result := v1 + v2;
end;
2018-04-17 19:07:59 +03:00
end;*)
SumTC[integer] = instance
function sum(v1, v2: integer): integer;
begin
Result := v1 + v2;
end;
2018-04-10 20:19:28 +03:00
end;
2018-04-13 18:25:29 +03:00
(*
2018-04-10 20:19:28 +03:00
ConceptSingleton<T> = class where T: constructor;
class _instance: T;
class inited: boolean := false;
public
class function &Instance: T;
begin
if inited = false then
begin
inited := true;
_instance := new T;
end;
Result := _instance;
end;
2018-04-13 18:25:29 +03:00
end;*)
2018-04-10 20:19:28 +03:00
function Sum3<T, SumT>(v1, v2, v3: T): T; where SumT: SumTC<T>, constructor;
begin
2018-04-13 18:25:29 +03:00
var s := __ConceptSingleton&<SumT>.&Instance;
2018-04-10 20:19:28 +03:00
Result := s.sum(v1, s.sum(v2, v3));
end;
begin
2018-04-17 19:07:59 +03:00
write(Sum3&<integer, SumTC_integer>(1, 2, 3));
2018-04-10 20:19:28 +03:00
end.