Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generic programming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=====In Free Pascal===== [[Free Pascal]] implemented generics in 2006 in [[Free Pascal#Version 2.2.x|version 2.2.0]], before Delphi and with different syntax and semantics. However, since FPC version 2.6.0, the Delphi-style syntax is available when using the language mode <code>{$mode Delphi}</code>. Thus, Free Pascal code supports generics in either style. Delphi and Free Pascal example: <syntaxhighlight lang="delphi"> // Delphi style unit A; {$ifdef fpc} {$mode delphi} {$endif} interface type TGenericClass<T> = class function Foo(const AValue: T): T; end; implementation function TGenericClass<T>.Foo(const AValue: T): T; begin Result := AValue + AValue; end; end. // Free Pascal's ObjFPC style unit B; {$ifdef fpc} {$mode objfpc} {$endif} interface type generic TGenericClass<T> = class function Foo(const AValue: T): T; end; implementation function TGenericClass.Foo(const AValue: T): T; begin Result := AValue + AValue; end; end. // example usage, Delphi style program TestGenDelphi; {$ifdef fpc} {$mode delphi} {$endif} uses A,B; var GC1: A.TGenericClass<Integer>; GC2: B.TGenericClass<String>; begin GC1 := A.TGenericClass<Integer>.Create; GC2 := B.TGenericClass<String>.Create; WriteLn(GC1.Foo(100)); // 200 WriteLn(GC2.Foo('hello')); // hellohello GC1.Free; GC2.Free; end. // example usage, ObjFPC style program TestGenDelphi; {$ifdef fpc} {$mode objfpc} {$endif} uses A,B; // required in ObjFPC type TAGenericClassInt = specialize A.TGenericClass<Integer>; TBGenericClassString = specialize B.TGenericClass<String>; var GC1: TAGenericClassInt; GC2: TBGenericClassString; begin GC1 := TAGenericClassInt.Create; GC2 := TBGenericClassString.Create; WriteLn(GC1.Foo(100)); // 200 WriteLn(GC2.Foo('hello')); // hellohello GC1.Free; GC2.Free; end. </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)