# Generics

**URL:** https://talk.remobjects.com/t/generics/12512
**Category:** Oxygene
**Tags:** island, oxygene
**Created:** [March 22, 2017, 10:15am UTC](https://talk.remobjects.com/t/generics/12512 "2017-03-22T10:15:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dfndr](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@dfndr](https://talk.remobjects.com/u/dfndr)
#### Post date: [March 22, 2017, 10:15am UTC](https://talk.remobjects.com/t/generics/12512/1 "2017-03-22T10:15:05Z")

</div>

Can Island compile these examples that work in FPC or Delphi? Thank.

```
program FPC;

{$MODE DELPHI}

procedure QuickSort<T>(const A: ^T; L, R: T);
begin
  if L < R then
  WriteLn('comparison L < R');
end;

begin
end.

program Delphi;

uses
  System.SysUtils;

type
  TArray<T> = record
    procedure QuickSort(const A: TArray<T>; L, R: T);
  end;

procedure TArray<T>.QuickSort(const A: TArray<T>; L, R: T);
begin
  case GetTypeKind(T) of
    tkInteger: WriteLn('Using type Integer');
    tkString: Writeln('Using type String');
  end;
end;

begin
end.
```

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [March 22, 2017, 10:25am UTC](https://talk.remobjects.com/t/generics/12512/2 "2017-03-22T10:25:15Z")

</div>

You can use something like

```auto
namespace ConsoleApplication4;

interface

type
  TArray<T> = record 
  public
    procedure QuickSort(const A: TArray<T>; L, R: T);
  end;

implementation

method TArray<T>.QuickSort(const A: TArray<T>; L: T; R: T);
begin
  if typeof(T) = typeof(Integer) then
    WriteLn('Using type Integer')
  else if typeof(T) = typeof(string) then
    Writeln('Using type String');
end;

end.

```

---

<div class="post-metadata">

### Author: ![dfndr](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@dfndr](https://talk.remobjects.com/u/dfndr)
#### Post date: [March 22, 2017, 10:53am UTC](https://talk.remobjects.com/t/generics/12512/3 "2017-03-22T10:53:17Z")

</div>

Won’t this produce unnecessary slow code?

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [March 22, 2017, 11:45am UTC](https://talk.remobjects.com/t/generics/12512/4 "2017-03-22T11:45:16Z")

</div>

In optimized mode it will get rid of the branches that can’t be hit.

---

<div class="post-metadata">

### Author: ![dfndr](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@dfndr](https://talk.remobjects.com/u/dfndr)
#### Post date: [March 22, 2017, 11:51am UTC](https://talk.remobjects.com/t/generics/12512/5 "2017-03-22T11:51:25Z")

</div>

Ah yes Island is multi-stage compiler so of course it would optimize this… 😃
