Nullable types/fi
English · Suomi
Tyhjennettävät tyypit (Nullable types) ovat sellaisia tietotyyppejä, joilla ei välttämättä ole arvoa (voidaan määrätä). Yksi tällainen tyyppi Pascalissa on pointer-tyyppi, jolla voi olla nil arvo, mikä tarkoittaa, että sitä ei ole osoitettu mihinkään tiettyyn osoitteeseen. Sama käyttäytyminen voidaan toteuttaa käyttämällä geneerisiä tyyppejä ja advanced record tietueita, joissa on operaattorin ylikuormitus.
unit Nullable;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils;
type
TNullable<T> = record
private
FHasValue: Boolean;
FValue: T;
function GetValue: T;
procedure SetValue(AValue: T);
public
procedure Clear;
property HasValue: Boolean read FHasValue;
property Value: T read GetValue write SetValue;
class operator Implicit(A: T): TNullable<T>;
class operator Implicit(A: Pointer): TNullable<T>;
end;
implementation
{ TNullable }
function TNullable<T>.GetValue: T;
begin
if FHasValue then
Result := FValue
else
raise Exception.Create('Variable has no value');
end;
procedure TNullable<T>.SetValue(AValue: T);
begin
FValue := AValue;
FHasValue := True;
end;
procedure TNullable<T>.Clear;
begin
FHasValue := False;
end;
class operator TNullable<T>.Implicit(A: T): TNullable<T>;
begin
Result.Value := A;
end;
class operator TNullable<T>.Implicit(A: Pointer): TNullable<T>;
begin
if A = nil then Result.Clear
else raise Exception.Create('Pointer value not allowed');
end;
end.Tyhjennettävät tyypit (Nullable types) voidaan määritellä esimerkiksi näin:
NullableChar = TNullable<Char>;
NullableInteger = TNullable<Integer>;
NullableString = TNullable<string>;Katso myös
Ulkopuoliset linkit
From the Free Pascal wiki, page "Nullable types/fi", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 2 people, 2019–2022
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Djzepi | Created page | |
| Kai Burghardt | resolve Category: Pages using deprecated enclose attributes (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
