J'essaie d'utiliser une chaîne dans une déclaration de cas, mais il me donne expected a discrete type. Found type Standard.String
Je comprends que les chaînes ne sont pas discrètes. Je me demande s'il y a un travail ou non. Voici mon code:Déclaration de cas Ada avec des chaînes
function Is_Valid_Direction(Direction_To_Go : in String) return Integer is
Room : Integer := 0;
begin
--if (Direction_To_Go = "NORTH" or Direction_To_Go = "N") then
-- Room := Building(currentRoom).exits(NORTH);
--elsif (Direction_To_Go = "SOUTH" or Direction_To_Go = "S") then
-- Room := Building(currentRoom).exits(SOUTH);
--elsif (Direction_To_Go = "EAST" or Direction_To_Go = "E") then
-- Room := Building(currentRoom).exits(EAST);
--elsif (Direction_To_Go = "WEST" or Direction_To_Go = "W") then
-- Room := Building(currentRoom).exits(WEST);
--elsif (Direction_To_Go = "UP" or Direction_To_Go = "U") then
-- Room := Building(currentRoom).exits(UP);
--elsif (Direction_To_Go = "DOWN" or Direction_To_Go = "D") then
-- Room := Building(currentRoom).exits(DOWN);
--end if;
case Direction_To_Go is
when "NORTH" | "N" => Room := Building(currentRoom).exits(NORTH);
when "SOUTH" | "S" => Room := Building(currentRoom).exits(SOUTH);
when "EAST" | "E" => Room := Building(currentRoom).exits(EAST);
when "WEST" | "W" => Room := Building(currentRoom).exits(WEST);
when "UP" | "U" => Room := Building(currentRoom).exits(UP);
when "DOWN" | "D" => Room := Building(currentRoom).exits(DOWN);
when others => Room := 0;
end case;
return Room;
end Is_Valid_Direction;
La section commentée fait exactement ce que je veux, mais avec des instructions if. J'essaie juste de voir si c'est possible avec une déclaration de cas.
Non. Ada n'est pas Java. – ajb
Il peut être utile de réfléchir aux choses «à l'envers» - commencer par les internes, puis aller à l'interface utilisateur - plus précisément, réfléchir sur le problème en termes de types. http://blog.kickin-the-darkness.com/2007/08/fundamental-theory-of-ada.html – Shark8