-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Variant records and related concepts
- Loading branch information
1 parent
17e9337
commit d75359a
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif} | ||
|
||
uses SysUtils; | ||
|
||
{ Example function that concatenates all elements of an array of const | ||
into a String. } | ||
function GlueEverything(const MyArray: array of const): String; | ||
var | ||
I: Integer; | ||
begin | ||
Result := ''; | ||
|
||
for I := 0 to High(MyArray) do | ||
begin | ||
// treat MyArray[I] as TVarRec, check for type and do something | ||
case MyArray[I].VType of | ||
vtInteger: | ||
begin | ||
Writeln('Integer: ', MyArray[I].VInteger); | ||
Result := Result + IntToStr(MyArray[I].VInteger) + ' '; | ||
end; | ||
vtAnsiString: | ||
begin | ||
Writeln('Ansi String (8-bit chars): ', AnsiString(MyArray[I].VAnsiString)); | ||
Result := Result + AnsiString(MyArray[I].VAnsiString) + ' '; | ||
end; | ||
vtUnicodeString: | ||
begin | ||
Writeln('Unicode String (16-bit chars): ', UnicodeString(MyArray[I].VUnicodeString)); | ||
Result := Result + UnicodeString(MyArray[I].VUnicodeString) + ' '; | ||
end; | ||
else | ||
Writeln('Something else, ignoring'); | ||
end; | ||
end; | ||
end; | ||
|
||
var | ||
S: String; | ||
begin | ||
S := GlueEverything([123, 'Hello', 'World', 456]); | ||
Writeln(S); | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif} | ||
|
||
type | ||
TVector2 = packed record | ||
case Integer of | ||
0: (X, Y: Single); | ||
1: (Data: array [0..1] of Single); | ||
end; | ||
|
||
TVector3 = packed record | ||
case Integer of | ||
0: (X, Y, Z: Single); | ||
1: (Data: array [0..2] of Single); | ||
2: (XY: TVector2); | ||
end; | ||
|
||
|
||
var | ||
V2: TVector2; | ||
V: TVector3; | ||
I: Integer; | ||
begin | ||
Writeln('Size of TVector2 is ', SizeOf(TVector2)); | ||
Writeln(' Should be equal to ', SizeOf(Single) * 2); | ||
|
||
Writeln('Size of TVector3 is ', SizeOf(TVector3)); | ||
Writeln(' Should be equal to ', SizeOf(Single) * 3); | ||
|
||
V.X := 1; | ||
V.Y := 2; | ||
V.Z := 3; | ||
|
||
for I := 0 to 2 do | ||
Writeln('V.Data[', I, '] is ', V.Data[I]:1:2); | ||
|
||
V2 := V.XY; | ||
|
||
for I := 0 to 1 do | ||
Writeln('V2.Data[', I, '] is ', V2.Data[I]:1:2); | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif} | ||
|
||
uses Variants; | ||
var | ||
V1, V2, V3: Variant; | ||
begin | ||
V1 := 'My String'; | ||
V1 := 123; // V1 no longer holds String, it has Integer now | ||
V2 := 456.789; | ||
V3 := V1 + V2; // result is float | ||
Writeln('V3 = ', V3); | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters