Recherche…


Introduction

Les attributs de sous-type 'Image et 'Value prendront respectivement une valeur scalaire et une chaîne et renverront respectivement une chaîne et une valeur scalaire. Le résultat de 'Image peut être entré dans 'Value pour obtenir la valeur d'origine. L'inverse est également vrai.

L'attribut __Scalar_Object__'Image peut être utilisé directement sur les objets (depuis Ada 2012-TC-1).

Syntaxe

  • function Scalar'Image (Argument: Scalar'Base) retourne String;
  • function Discrete'Image (Argument: Discrete'Base) retourne String;
  • function Integer'Image (Argument: Integer'Base) retourne String;
  • function Enumeration'Image (Argument: Enumeration'Base) return String;
  • function Real'Image (Argument: Real'Base) retourne String;
  • function Numeric'Image (Argument: Numeric'Base) retourne String;
  • fonction Scalar'Value (Argument: String) renvoie Scalar'Base;
  • function Discrete'Value (Argument: String) retourne Discrete'Base;
  • function Integer'Value (Argument: String) retourne Integer'Base;
  • function Enumeration'Value (Argument: String) renvoie Enumeration'Base;
  • function Real'Value (Argument: String) renvoie Real'Base;
  • function Scalar_Object 'Image return String;

Remarques

Notez que 'Image peut entraîner des résultats définis par l'implémentation (RM 3.5), à savoir lorsque certains caractères graphiques nécessaires au résultat String ne sont pas définis dans Character . Considérons les plus grands répertoires de 'Wide_Image et 'Wide_Wide_Image .

Ada 2012 (TC-1)

L'autorisation d'utiliser l'attribut __Scalar_Object__'Image directement sur un objet a été ajoutée dans Ada 2012-TC-1 (avril 2016).

Imprimez float en utilisant l'attribut Image

Ada 2012 (TC-1)
with Ada.Text_IO;

procedure Main is
   type Some_Float digits 8 range 0.0 .. 10.0;
   X : Some_Float := 2.71;
begin
   Ada.Text_IO.Put_Line (X'Image);
end Main;

Résultat

2.71000E+00

Imprimer un entier en utilisant l'attribut Image

Ada 2012 (TC-1)
with Ada.Text_IO;

procedure Main is
   type Some_Integer is range -42 .. 42;
   X : Some_Integer := 17;
begin
   Ada.Text_IO.Put_Line (X'Image);
end Main;

Résultat

17

Imprimer l'énumération à l'aide de l'attribut Image

Ada 2012 (TC-1)
with Ada.Text_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   X : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (X'Image);
   Ada.Text_IO.Put_Line (Pear'Image);
end Main;

Résultat

ORANGE
PEAR

Imprimer l'énumération à l'aide de l'attribut Image

with Ada.Text_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   X : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (Fruit'Image (X));
end Main;

Résultat

ORANGE

Imprimer Integer en utilisant l'attribut Image

with Ada.Text_IO;

procedure Main is
   X : Integer := 17;
begin
   Ada.Text_IO.Put_Line (Integer'Image (X));
end Main;

Résultat

17

Imprimer Float en utilisant l'attribut Image

with Ada.Text_IO;

procedure Main is
   X : Float := 2.71;
begin
   Ada.Text_IO.Put_Line (Float'Image (X));
end Main;

Résultat

2.71000E+00

En tant qu'inverses

with Ada.Text_IO;

procedure Image_And_Value is
   type Fruit is (Banana, Orange, Pear);
   X  : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (Boolean'Image
      (Fruit'Value (Fruit'Image (X)) = X
          and
       Fruit'Image (Fruit'Value ("ORANGE")) = "ORANGE"));
end Image_And_Value;

Résultat

TRUE


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow