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 Delphi===== The [[Object Pascal]] dialect [[Delphi (software)|Delphi]] acquired generics in the 2007 Delphi 11 release by [[CodeGear]], initially only with the .NET compiler (since discontinued) before being added to the native code in the 2009 Delphi 12 release. The semantics and abilities of Delphi generics are largely modelled on those of generics in .NET 2.0, though the implementation is by necessity quite different. Here is a more or less direct translation of the first C# example shown above: <syntaxhighlight lang="delphi"> program Sample; {$APPTYPE CONSOLE} uses Generics.Defaults; //for IComparer<> type TUtils = class class procedure MakeAtLeast<T>(Arr: TArray<T>; const Lowest: T; Comparer: IComparer<T>); overload; class procedure MakeAtLeast<T>(Arr: TArray<T>; const Lowest: T); overload; end; class procedure TUtils.MakeAtLeast<T>(Arr: TArray<T>; const Lowest: T; Comparer: IComparer<T>); var I: Integer; begin if Comparer = nil then Comparer := TComparer<T>.Default; for I := Low(Arr) to High(Arr) do if Comparer.Compare(Arr[I], Lowest) < 0 then Arr[I] := Lowest; end; class procedure TUtils.MakeAtLeast<T>(Arr: TArray<T>; const Lowest: T); begin MakeAtLeast<T>(Arr, Lowest, nil); end; var Ints: TArray<Integer>; Value: Integer; begin Ints := TArray<Integer>.Create(0, 1, 2, 3); TUtils.MakeAtLeast<Integer>(Ints, 2); for Value in Ints do WriteLn(Value); ReadLn; end. </syntaxhighlight> As with C#, methods and whole types can have one or more type parameters. In the example, TArray is a generic type (defined by the language) and MakeAtLeast a generic method. The available constraints are very similar to the available constraints in C#: any value type, any class, a specific class or interface, and a class with a parameterless constructor. Multiple constraints act as an additive union.
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)