2

I need to verify a variable is a certain type.

Is there a way to check the type of a variable in Ada?

I've tried looking at Ada attributes but didn't see anything.

Thomas Baruchel
  • 7,236
  • 2
  • 27
  • 46
Bakuto
  • 21
  • 1
  • 2
    This answer will help you understand why its not needed. http://stackoverflow.com/a/29406543/2545197 – Abhinay Jan 16 '16 at 19:47
  • 3
    Possible duplicate of [Datatype of variable in ada](http://stackoverflow.com/questions/29405634/datatype-of-variable-in-ada) – AnthonyW Jan 16 '16 at 21:12
  • 1
    It would help to post some code, and include some "pseudo-code" or a comment at the point where you're trying to "check the type of a variable". That way, we can look at it and figure out whether what you're trying to do makes sense in Ada, and what the correct way to do it would be. – ajb Jan 18 '16 at 03:20

1 Answers1

2

Ada is a strongly typed language so there is really no need to have a function to return the variable's type, as there is in Python or Ruby (duck typed languages) because when you declare a variable you specify its type. The program already knows its type.

If a variable X is declared with type T'Class, then the type of the actual value can be T or any type derived from T. In that case, you can use X'Tag to get the tag of the value's actual type, which is the closest you can come to getting the actual type. Once you have a tag, you can do things like getting the type's name (there are functions for this in Ada.Tags), comparing it to the tag of some type to see if it's that type, etc. But Integer is not a tagged type, so you can't use 'Tag on it and there would be no use for it because it is a primitive type.

Billy Ferguson
  • 1,429
  • 11
  • 23