mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-20 22:59:54 +02:00
codetools: examples for records with methods and delphi generics
git-svn-id: trunk@29344 -
This commit is contained in:
parent
89410a21f9
commit
4c2095856b
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -465,6 +465,7 @@ components/codetools/examples/scanexamples/brokenincfiles.inc svneol=native#text
|
||||
components/codetools/examples/scanexamples/completion1.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/empty.inc svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/emptymethods1.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/genericsexample.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/getcontextexample.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/identcomplexample.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/indentation.pas svneol=native#text/plain
|
||||
@ -475,6 +476,7 @@ components/codetools/examples/scanexamples/nestedclasses.pas svneol=native#text/
|
||||
components/codetools/examples/scanexamples/objctest1.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/overloadedfunction.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/publishedvars.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/recordsexample.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/resourcetest1.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/simpleunit1.pas svneol=native#text/plain
|
||||
components/codetools/examples/scanexamples/test.h svneol=native#text/plain
|
||||
|
@ -0,0 +1,31 @@
|
||||
program GenericExamples;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
// generics
|
||||
type
|
||||
generic TArr<T> = array[0..2] of T;
|
||||
generic TProc<T> = procedure(Arg: T);
|
||||
generic TRecG<T> = record
|
||||
F: T;
|
||||
end;
|
||||
TGenericArray<T> = array of T;
|
||||
IGenericInterface<T> = interface
|
||||
function DoSomething(Arg: T): T;
|
||||
end;
|
||||
TGenericClass<T1,T2> = class(TInterfacedObject, IGenericInterface<T1>)
|
||||
F: T2;
|
||||
type
|
||||
Intf = IGenericInterface<Integer>;
|
||||
function DoSomething(Arg: T1): T2;
|
||||
function Test(Arg: Intf): Intf;
|
||||
end;
|
||||
|
||||
var
|
||||
ArraySpecialize: TGenericArray<Integer>;
|
||||
ClassSpecialize: TGenericClass<Integer,String>;
|
||||
|
||||
begin
|
||||
FooInt := TGenericClass<Integer,String>.Create;
|
||||
end.
|
||||
|
@ -1,9 +1,8 @@
|
||||
program NestedClasses;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
{$modeswitch advancedrecords} // {$mode delphi} has it automatically
|
||||
|
||||
// 1. advanced classes/objects
|
||||
// nested classes/objects
|
||||
|
||||
type
|
||||
TClass1 = class
|
||||
@ -23,76 +22,6 @@ begin
|
||||
WriteLn(F);
|
||||
end;
|
||||
|
||||
// 2. advanced records
|
||||
type
|
||||
TRec1 = record
|
||||
hWnd : HWND;
|
||||
private
|
||||
F1: Integer;
|
||||
F2: Byte;
|
||||
public
|
||||
type
|
||||
TBar = Integer;
|
||||
const
|
||||
C: TBar = 1;
|
||||
var
|
||||
F3: TBar;
|
||||
F4: Byte;
|
||||
class var
|
||||
F5: TBar;
|
||||
private
|
||||
type
|
||||
Int = Integer;
|
||||
var
|
||||
F: Int;
|
||||
const
|
||||
DefaultF: Int = 1;
|
||||
public
|
||||
function GetF: Int;
|
||||
procedure SetF(const Value: Int);
|
||||
// full list of operators see in tests/test/terecs6.pp
|
||||
class operator Inc(Rec: TRec1): TRec1;
|
||||
end;
|
||||
|
||||
function TRec1.GetF: Int;
|
||||
begin
|
||||
Result := F;
|
||||
end;
|
||||
|
||||
procedure TRec1.SetF(const Value: Int);
|
||||
begin
|
||||
F := Value;
|
||||
end;
|
||||
|
||||
class operator TRec1.Inc(Rec: TRec1): TRec1;
|
||||
begin
|
||||
Result.F := Rec.F + 1;
|
||||
end;
|
||||
|
||||
// 3. generics
|
||||
type
|
||||
generic TArr<T> = array[0..2] of T;
|
||||
generic TProc<T> = procedure(Arg: T);
|
||||
generic TRecG<T> = record
|
||||
F: T;
|
||||
end;
|
||||
TGenericArray<T> = array of T;
|
||||
IGenericInterface<T> = interface
|
||||
function DoSomething(Arg: T): T;
|
||||
end;
|
||||
TGenericClass<T1,T2> = class(TInterfacedObject, IGenericInterface<T1>)
|
||||
F: T2;
|
||||
type
|
||||
Intf = IGenericInterface<Integer>;
|
||||
function DoSomething(Arg: T1): T2;
|
||||
function Test(Arg: Intf): Intf;
|
||||
end;
|
||||
|
||||
var
|
||||
ArraySpecialize: TGenericArray<Integer>;
|
||||
ClassSpecialize: TGenericClass<Integer,String>;
|
||||
|
||||
begin
|
||||
FooInt := TGenericClass<Integer,String>.Create;
|
||||
end.
|
||||
|
||||
|
@ -0,0 +1,55 @@
|
||||
program RecordsExample;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
{$modeswitch advancedrecords} // {$mode delphi} has it automatically
|
||||
|
||||
// advanced records
|
||||
type
|
||||
TRec1 = record
|
||||
hWnd : HWND;
|
||||
private
|
||||
F1: Integer;
|
||||
F2: Byte;
|
||||
public
|
||||
type
|
||||
TBar = Integer;
|
||||
const
|
||||
C: TBar = 1;
|
||||
var
|
||||
F3: TBar;
|
||||
F4: Byte;
|
||||
class var
|
||||
F5: TBar;
|
||||
private
|
||||
type
|
||||
Int = Integer;
|
||||
var
|
||||
F: Int;
|
||||
const
|
||||
DefaultF: Int = 1;
|
||||
public
|
||||
function GetF: Int;
|
||||
procedure SetF(const Value: Int);
|
||||
// full list of operators see in tests/test/terecs6.pp
|
||||
class operator Inc(Rec: TRec1): TRec1;
|
||||
end;
|
||||
|
||||
function TRec1.GetF: Int;
|
||||
begin
|
||||
Result := F;
|
||||
end;
|
||||
|
||||
procedure TRec1.SetF(const Value: Int);
|
||||
begin
|
||||
F := Value;
|
||||
end;
|
||||
|
||||
class operator TRec1.Inc(Rec: TRec1): TRec1;
|
||||
begin
|
||||
Result.F := Rec.F + 1;
|
||||
end;
|
||||
|
||||
|
||||
begin
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user