AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / coding / Perguntas / 79552245
Accepted
GuidoG
GuidoG
Asked: 2025-04-03 15:17:07 +0800 CST2025-04-03 15:17:07 +0800 CST 2025-04-03 15:17:07 +0800 CST

Como definir um inteiro anulável como nulo?

  • 772

Criei um tipo anulável como este, que encontrei em uma resposta do SO, não lembro qual.

unit NullableType;

interface

uses
  System.SysUtils, System.Rtti;

type
  TNullable<T> = record
 private
    FValue: T;
    FHasValue: IInterface;
    function GetHasValue: Boolean;
    function GetValue: T;
    procedure SetValue(const AValue: T);
  public
    constructor Create(AValue: T);
    function ToString: string; // <-- add this for easier use!
    property HasValue: Boolean read GetHasValue;
    property Value: T read GetValue write SetValue;
  end;


implementation


constructor TNullable<T>.Create(AValue: T);
begin
   SetValue(AValue);
end;

function TNullable<T>.GetHasValue: Boolean;
begin
  Result := FHasValue <> nil;
end;

function TNullable<T>.GetValue: T;
begin
  if HasValue then
    Result := FValue
  else
    Result := Default(T);
end;

procedure TNullable<T>.SetValue(const AValue: T);
begin
  FValue := AValue;
  FHasValue := TInterfacedObject.Create;
end;

function TNullable<T>.ToString: string;
begin
  if HasValue then
  begin
    if TypeInfo(T) = TypeInfo(TDateTime) then
      Result := DateTimeToStr(PDateTime(@FValue)^)
    else if TypeInfo(T) = TypeInfo(TDate) then
      Result := DateToStr(PDateTime(@FValue)^)
    else if TypeInfo(T) = TypeInfo(TTime) then
      Result := TimeToStr(PDateTime(@FValue)^)
    else
      Result := TValue.From<T>(FValue).ToString;
  end
  else
    Result := 'null';
end;

end.

Meu problema é que não sei como defini-lo como nulo.
Por exemplo

var
  id : TNullable<integer>;
begin
  if Edit1.Text <> '' then
    id.Value := StrToInt(Edit1.Text)
  else
    id.Value := null;  // runtime error

isso me dá erro de tempo de execução

Não foi possível converter a variante do tipo (Null) em tipo (Integer)

Já faz um tempo que programo em Delphi e não consigo descobrir como definir o valor da idvariável paranull

id.Value := nil;

dá erro de compilador

Tipos incompatíveis: 'integer' e 'pointer'

Somente não definindo o valor de ideu consigo ter seu valor null, mas e se eu quiser defini-lo para qualquer valor, incluindo null? Como fazer isso?

delphi
  • 2 2 respostas
  • 120 Views

2 respostas

  • Voted
  1. Remy Lebeau
    2025-04-04T01:05:44+08:002025-04-04T01:05:44+08:00

    nullé um const Variantdo tipo VT_NULL, é por isso que você está recebendo um erro de tempo de execução relacionado a uma Variantconversão. Você quer nilem vez disso.

    No entanto, você não pode atribuir a nilpara a Tquando Tnão for um tipo de ponteiro. Então, para fazer o que você quer, você precisa atualizar Nullable<T>para aceitar T^ponteiros como entrada.

    Do Delphi 2006 em diante, um recordpode sobrecarregar operadores , então você não precisa aceitar atribuições por meio de uma Valuepropriedade. Você pode sobrecarregar operadores de conversão que permitirão que você converta Tvalores e T^ponteiros em Nullable<T>(e converta Nullable<T>em Tvalores), então você poderá atribuir nilponteiros para suas Nullablevariáveis, por exemplo:

    unit NullableType;
    
    interface
    
    type
      TNullable<T> = record
      public
        type PointerOfT = ^T; // <-- add this
      private
        FValue: T;
        FHasValue: IInterface;
    
        function GetHasValue: Boolean;
    
        procedure SetValue(const AValue: T);
        procedure SetValueByPointer(const AValue: PointerOfT);
        procedure SetToNil;
      public
        constructor Create(const AValue: T); overload;
        constructor Create(const AValue: PointerOfT); overload; // <-- add this
    
        // add these...
        class operator Implicit(const Src: TNullable<T>): T;
        class operator Implicit(const Src: T): TNullable<T>;
        class operator Implicit(const Src: PointerOfT): TNullable<T>;
    
        class operator Explicit(const Src: TNullable<T>): T;
        class operator Explicit(const Src: T): TNullable<T>;
        class operator Explicit(const Src: PointerOfT): TNullable<T>;
        //
    
        property HasValue: Boolean read GetHasValue;
        property Value: T read FValue write SetValue; // <-- optional now!
    
        function ToString: string;
      end;
    
    implementation
    
    uses
      System.SysUtils, System.Rtti;
    
    constructor TNullable<T>.Create(const AValue: T);
    begin
      SetValue(AValue);
    end;
    
    constructor TNullable<T>.Create(const AValue: PointerOfT);
    begin
      SetValueByPointer(AValue);
    end;
    
    class operator TNullable<T>.Implicit(const Src: TNullable<T>): T;
    begin
      Result := Src.FValue;
    end;
    
    class operator TNullable<T>.Implicit(const Src: T): TNullable<T>;
    begin
      Result.SetValue(Src);
    end;
    
    class operator TNullable<T>.Implicit(const Src: PointerOfT): TNullable<T>;
    begin
      Result.SetValueByPointer(Src);
    end;
    
    class operator TNullable<T>.Explicit(const Src: TNullable<T>): T;
    begin
      Result := Src.FValue;
    end;
    
    class operator TNullable<T>.Explicit(const Src: T): TNullable<T>;
    begin
      Result.SetValue(Src);
    end;
    
    class operator TNullable<T>.Explicit(const Src: PointerOfT): TNullable<T>;
    begin
      Result.SetValueByPointer(Src);
    end;
    
    procedure TNullable<T>.SetValue(const AValue: T);
    begin
      FValue := AValue;
      FHasValue := TInterfacedObject.Create;
    end;
    
    procedure TNullable<T>.SetValueByPointer(const AValue: PointerOfT);
    begin
      if AValue <> nil then
        SetValue(AValue^)
      else
        SetToNil;
    end;
    
    procedure TNullable<T>.SetToNil;
    begin
      FValue := Default(T);
      FHasValue := nil;
    end;
    
    function TNullable<T>.ToString: string;
    begin
      if HasValue then
      begin
        if TypeInfo(T) = TypeInfo(TDateTime) then
          Result := DateTimeToStr(PDateTime(@FValue)^)
        else if TypeInfo(T) = TypeInfo(TDate) then
          Result := DateToStr(PDateTime(@FValue)^)
        else if TypeInfo(T) = TypeInfo(TTime) then
          Result := TimeToStr(PDateTime(@FValue)^)
        else
          Result := TValue.From<T>(FValue).ToString;
      end
      else
        Result := 'null';
    end;
    
    end.
    
    var
      id : TNullable<integer>;
    begin
      if Edit1.Text <> '' then
        id := StrToInt(Edit1.Text)
      else
        id := nil;
    

    Além disso, a partir do Delphi 10.4, você pode usar um Custom Managed Record para substituir o IInterfacepor um simples Boolean, por exemplo:

    • O uso de IInterfaceé baseado em um antigo artigo de blog escrito por Allen Bauer que antecede a introdução dos CMRs. Ele até afirma no artigo que os CMRs teriam resolvido o problema que ele estava usando IInterfacecomo solução alternativa!
    unit NullableType;
    
    interface
    
    type
      TNullable<T> = record
      public
        type PointerOfT = ^T;
      private
        FValue: T;
        FHasValue: Boolean; // <-- change this
    
        procedure SetValue(const AValue: T);
        procedure SetValueByPointer(const AValue: PointerOfT);
        procedure SetToNil;
      public
        constructor Create(const AValue: T); overload;
        constructor Create(const AValue: PointerOfT); overload;
    
        class operator Initialize(out Dest: TNullable<T>); // <-- add this
    
        class operator Implicit(const Src: TNullable<T>): T;
        class operator Implicit(const Src: T): TNullable<T>;
        class operator Implicit(const Src: PointerOfT): TNullable<T>;
        class operator Explicit(const Src: TNullable<T>): T;
        class operator Explicit(const Src: T): TNullable<T>;
        class operator Explicit(const Src: PointerOfT): TNullable<T>;
    
        property HasValue: Boolean read FHasValue;
        property Value: T read FValue write SetValue;
    
        function ToString: string;
      end;
    
    implementation
    
    uses
      System.SysUtils, System.Rtti;
    
    constructor TNullable<T>.Create(const AValue: T);
    begin
      SetValue(AValue);
    end;
    
    constructor TNullable<T>.Create(const AValue: PointerOfT);
    begin
      SetValueByPointer(AValue);
    end;
    
    class operator TNullable<T>.Initialize(out Dest: TNullable<T>);
    begin
      Dest.SetToNil;
    end;
    
    class operator TNullable<T>.Implicit(const Src: TNullable<T>): T;
    begin
      Result := Src.FValue;
    end;
    
    class operator TNullable<T>.Implicit(const Src: T): TNullable<T>;
    begin
      Result.SetValue(Src);
    end;
    
    class operator TNullable<T>.Implicit(const Src: PointerOfT): TNullable<T>;
    begin
      Result.SetValueByPointer(Src);
    end;
    
    class operator TNullable<T>.Explicit(const Src: TNullable<T>): T;
    begin
      Result := Src.FValue;
    end;
    
    class operator TNullable<T>.Explicit(const Src: T): TNullable<T>;
    begin
      Result.SetValue(Src);
    end;
    
    class operator TNullable<T>.Explicit(const Src: PointerOfT): TNullable<T>;
    begin
      Result.SetValueByPointer(Src);
    end;
    
    procedure TNullable<T>.SetValue(const AValue: T);
    begin
      FValue := AValue;
      FHasValue := True;
    end;
    
    procedure TNullable<T>.SetValueByPointer(const AValue: PointerOfT);
    begin
      if AValue <> nil then
        SetValue(AValue^)
      else
        SetToNil;
    end;
    
    procedure TNullable<T>.SetToNil;
    begin
      FValue := Default(T);
      FHasValue := False;
    end;
    
    function TNullable<T>.ToString: string;
    begin
      if HasValue then
      begin
        if TypeInfo(T) = TypeInfo(TDateTime) then
          Result := DateTimeToStr(PDateTime(@FValue)^)
        else if TypeInfo(T) = TypeInfo(TDate) then
          Result := DateToStr(PDateTime(@FValue)^)
        else if TypeInfo(T) = TypeInfo(TTime) then
          Result := TimeToStr(PDateTime(@FValue)^)
        else
          Result := TValue.From<T>(FValue).ToString;
      end
      else
        Result := '(null)';
    end;
    
    end.
    

    Se você precisar dar suporte a versões mais antigas do Delphi, basta IFDEFcodificar adequadamente.

    • 3
  2. Best Answer
    HeartWare
    2025-04-03T15:37:54+08:002025-04-03T15:37:54+08:00

    Já que você usa uma interface para detectar se tem um valor ou não, eu faria isso:

    PROCEDURE TNullable<T>.SetNull;
      BEGIN
        FHasValue:=NIL
      END;
    
    FUNCTION TNullable<T>.IsNull : BOOLEAN;
      BEGIN
        Result:=NOT Assigned(FHasValue)
      END;
    

    Você não pode fazer isso por atribuição (ou talvez você possa - declarar um operador de atribuição que aceite um valor de ponteiro, mas permita apenas NILcomo valor, e então chamar SetNull. Se você tentar atribuir um não NILponteiro, gere uma exceção).

    • 1

relate perguntas

  • Como removo a mensagem “versão de teste não licenciada” em um projeto TMS Web Core? [fechado]

  • Por que TIdHTTP.Head() está gerando uma exceção 'HTTP/1.1 406 Not Acceptable'?

  • TTreeView: como marcar/desmarcar SOMENTE filhos de um TTreeNode?

  • Como pesquisar a partir da posição onde o cursor está no momento?

  • Usando a linguagem Rust no Delphi/RAD Studio/C++ Builder? [fechado]

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Reformatar números, inserindo separadores em posições fixas

    • 6 respostas
  • Marko Smith

    Por que os conceitos do C++20 causam erros de restrição cíclica, enquanto o SFINAE antigo não?

    • 2 respostas
  • Marko Smith

    Problema com extensão desinstalada automaticamente do VScode (tema Material)

    • 2 respostas
  • Marko Smith

    Vue 3: Erro na criação "Identificador esperado, mas encontrado 'import'" [duplicado]

    • 1 respostas
  • Marko Smith

    Qual é o propósito de `enum class` com um tipo subjacente especificado, mas sem enumeradores?

    • 1 respostas
  • Marko Smith

    Como faço para corrigir um erro MODULE_NOT_FOUND para um módulo que não importei manualmente?

    • 6 respostas
  • Marko Smith

    `(expression, lvalue) = rvalue` é uma atribuição válida em C ou C++? Por que alguns compiladores aceitam/rejeitam isso?

    • 3 respostas
  • Marko Smith

    Um programa vazio que não faz nada em C++ precisa de um heap de 204 KB, mas não em C

    • 1 respostas
  • Marko Smith

    PowerBI atualmente quebrado com BigQuery: problema de driver Simba com atualização do Windows

    • 2 respostas
  • Marko Smith

    AdMob: MobileAds.initialize() - "java.lang.Integer não pode ser convertido em java.lang.String" para alguns dispositivos

    • 1 respostas
  • Martin Hope
    Fantastic Mr Fox Somente o tipo copiável não é aceito na implementação std::vector do MSVC 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant Encontre o próximo dia da semana usando o cronógrafo 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor O inicializador de membro do construtor pode incluir a inicialização de outro membro? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský Por que os conceitos do C++20 causam erros de restrição cíclica, enquanto o SFINAE antigo não? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul O C++20 mudou para permitir a conversão de `type(&)[N]` de matriz de limites conhecidos para `type(&)[]` de matriz de limites desconhecidos? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann Como/por que {2,3,10} e {x,3,10} com x=2 são ordenados de forma diferente? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller O ponto e vírgula agora é opcional em condicionais bash com [[ .. ]] na versão 5.2? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench Por que um traço duplo (--) faz com que esta cláusula MariaDB seja avaliada como verdadeira? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng Por que `dict(id=1, **{'id': 2})` às vezes gera `KeyError: 'id'` em vez de um TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob: MobileAds.initialize() - "java.lang.Integer não pode ser convertido em java.lang.String" para alguns dispositivos 2024-03-20 03:12:31 +0800 CST

Hot tag

python javascript c++ c# java typescript sql reactjs html

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve