Is there an inbuilt is integer function in sml?
I mean something like:
I have to read a number from file and display it as output if it integer and raise exception if the number is not integer. for example I have to check if the output of (Int.fromString()) is integer and display it if it is …(repeat above)
The type system will ensure that the types of the values a function is given match the type signature of the function.
That is, if you have a function that takes an integer as input, such as
Then
nwill always be an integer. It will not be possible to call the function with anything but an integer; it will give a type error.If you have a polymorphic function, such as
Then you cannot know what type the input is at runtime. All types of input will be treated the same.
You can, however, always restrict a polymorphic function to only work on a given type, by making the type explicit when defining the function:
You can see the difference between
pairandpairIntby comparing what you get from callingpair 5topairInt 5, andpair "foo"topairInt "foo".If you have an
int option, as is the case if you try to convert anstringto aintusingInt.fromString, you can extract theintin several ways. For how to do this, I refer you to the question “In smlnj how do you convert “string option” to “string”?“.