Function prototype: Difference between revisions
Content added Content deleted
(Added Wren) |
MaiconSoft (talk | contribs) (Added Delphi example) |
||
Line 348: | Line 348: | ||
void main() {}</lang> |
void main() {}</lang> |
||
=={{header|Delphi}}== |
|||
{{libheader| System.SysUtils}} |
|||
In Delphi, prototype function is named Class/Record Helper. For now, is not possible has more then one helper active in a same object, if two or more was declareted, just the present in the last Unit declareted will be active, the others will be ignored. In case two or more helpers was declareted in same Unit, just the last helper declareted will be active. Can not inherit record helpers, but class helper can be.<br> |
|||
Patten: " identifierName = record helper for TypeIdentifierName"<br> |
|||
See [http://docwiki.embarcadero.com/RADStudio/Sydney/en/Class_and_Record_Helpers_(Delphi) Documentation] for more details.<br> |
|||
<lang Delphi> |
|||
program Function_prototype; |
|||
{$APPTYPE CONSOLE} |
|||
uses |
|||
System.SysUtils; |
|||
type |
|||
TIntArray = TArray<Integer>; |
|||
TIntArrayHelper = record helper for TIntArray |
|||
const |
|||
DEFAULT_VALUE = -1; |
|||
// A prototype declaration for a function that does not require arguments |
|||
function ToString(): string; |
|||
// A prototype declaration for a function that requires two arguments |
|||
procedure Insert(Index: Integer; value: Integer); |
|||
// A prototype declaration for a function that utilizes varargs |
|||
// varargs is not available, but a equivalent is array of const |
|||
procedure From(Args: array of const); |
|||
//A prototype declaration for a function that utilizes optional arguments |
|||
procedure Delete(Index: Integer; Count: Integer = 1); |
|||
//A prototype declaration for a function that utilizes named parameters |
|||
// Named parameters is not supported in Delphi |
|||
//Example of prototype declarations for subroutines or procedures |
|||
//(if these differ from functions) |
|||
procedure Sqr; //Procedure return nothing |
|||
function Averange: double; //Function return a value |
|||
end; |
|||
{ TIntHelper } |
|||
function TIntArrayHelper.Averange: double; |
|||
begin |
|||
Result := 0; |
|||
for var e in self do |
|||
Result := Result + e; |
|||
Result := Result / Length(self); |
|||
end; |
|||
procedure TIntArrayHelper.Delete(Index, Count: Integer); |
|||
begin |
|||
System.Delete(self, Index, Count); |
|||
end; |
|||
procedure TIntArrayHelper.From(Args: array of const); |
|||
var |
|||
I, Count: Integer; |
|||
begin |
|||
Count := Length(Args); |
|||
SetLength(self, Count); |
|||
if Count = 0 then |
|||
exit; |
|||
for I := 0 to High(Args) do |
|||
with Args[I] do |
|||
case VType of |
|||
vtInteger: |
|||
self[I] := VInteger; |
|||
vtBoolean: |
|||
self[I] := ord(VBoolean); |
|||
vtChar, vtWideChar: |
|||
self[I] := StrToIntDef(string(VChar), DEFAULT_VALUE); |
|||
vtExtended: |
|||
self[I] := Round(VExtended^); |
|||
vtString: |
|||
self[I] := StrToIntDef(VString^, DEFAULT_VALUE); |
|||
vtPChar: |
|||
self[I] := StrToIntDef(VPChar, DEFAULT_VALUE); |
|||
vtObject: |
|||
self[I] := cardinal(VObject); |
|||
vtClass: |
|||
self[I] := cardinal(VClass); |
|||
vtAnsiString: |
|||
self[I] := StrToIntDef(string(VAnsiString), DEFAULT_VALUE); |
|||
vtCurrency: |
|||
self[I] := Round(VCurrency^); |
|||
vtVariant: |
|||
self[I] := Integer(VVariant^); |
|||
vtInt64: |
|||
self[I] := Integer(VInt64^); |
|||
vtUnicodeString: |
|||
self[I] := StrToIntDef(string(VUnicodeString), DEFAULT_VALUE); |
|||
end; |
|||
end; |
|||
procedure TIntArrayHelper.Insert(Index, value: Integer); |
|||
begin |
|||
system.Insert([value], self, Index); |
|||
end; |
|||
procedure TIntArrayHelper.Sqr; |
|||
begin |
|||
for var I := 0 to High(self) do |
|||
Self[I] := Self[I] * Self[I]; |
|||
end; |
|||
function TIntArrayHelper.ToString: string; |
|||
begin |
|||
Result := '['; |
|||
for var e in self do |
|||
Result := Result + e.ToString + ', '; |
|||
Result := Result + ']'; |
|||
end; |
|||
begin |
|||
var val: TArray<Integer>; |
|||
val.From([1, '2', PI]); |
|||
val.Insert(0, -1); // insert -1 at position 0 |
|||
writeln(' Array: ', val.ToString, ' '); |
|||
writeln(' Averange: ', val.Averange: 3: 2); |
|||
val.Sqr; |
|||
writeln(' Sqr: ', val.ToString); |
|||
Readln; |
|||
end.</lang> |
|||
{{out}} |
|||
<pre> Array: [-1, 1, 2, 3, ] |
|||
Averange: 1.25 |
|||
Sqr: [1, 1, 4, 9, ]</pre> |
|||
Class helper example, with inherited helpers:<br> |
|||
Patten: " identifierName = class helper (ancestor list*) for TypeIdentifierName"<br> |
|||
Ancertor list is optional, and only will be used if this helper will inhret a parent helper. |
|||
{{libheader| System.SysUtils}} |
|||
{{libheader| System.Classes}} |
|||
<lang Delphi> |
|||
program Function_prototype_class; |
|||
{$APPTYPE CONSOLE} |
|||
uses |
|||
System.SysUtils, |
|||
System.Classes; |
|||
type |
|||
TStringListHelper1 = class helper for TStringList |
|||
constructor Create(FileName: TFileName); overload; |
|||
end; |
|||
TStringListHelper2 = class helper (TStringListHelper1) for TStringList |
|||
procedure SaveAndFree(FileName: TFileName); |
|||
end; |
|||
TStringListHelper3 = class helper (TStringListHelper2) for TStringList |
|||
procedure AddDateTime; |
|||
end; |
|||
{ TStringListHelper1 } |
|||
constructor TStringListHelper1.Create(FileName: TFileName); |
|||
begin |
|||
inherited Create; |
|||
if FileExists(FileName) then |
|||
LoadFromFile(FileName); |
|||
end; |
|||
{ TStringListHelper2 } |
|||
procedure TStringListHelper2.SaveAndFree(FileName: TFileName); |
|||
begin |
|||
SaveToFile(FileName); |
|||
Free; |
|||
end; |
|||
{ TStringListHelper3 } |
|||
procedure TStringListHelper3.AddDateTime; |
|||
begin |
|||
self.Add(DateToStr(now)); |
|||
end; |
|||
begin |
|||
with TStringList.Create('d:\Text.txt') do |
|||
begin |
|||
AddDateTime; |
|||
SaveAndFree('d:\Text_done.txt'); |
|||
end; |
|||
readln; |
|||
end.</lang> |
|||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |