|
Re: [eol][evl]Handling exception in casting types [message #763419 is a reply to message #763322] |
Fri, 09 December 2011 17:11 |
Steffen Zschaler Messages: 266 Registered: July 2009 |
Senior Member |
|
|
Hi Paulo,
The code you have written would not check the validity of the string. If
asInteger() is successful it will, per definition, return an integer. So
calling isKindOf (Integer) on it is redundant!
You will probably need to build a helper class of your own to provide
appropriate testing and conversion methods. Check out the 'tools'
concept in the Epsilon book.
Cheers,
Steffen
On 09/12/2011 15:09, Paulo Alexandre wrote:
> hi everybody
>
> I have a class with a <<value>> string field. The purpose of this
> field is to receive any input data (integers, booleans, reals and
> strings) and then casting to the desiredtype (like Integer, Boolean,
> Real ...).
>
> The casting (conversion) of this <<value>> field is happening in a
> .evl file.
>
> In EVL i'm trying to know if the content of <<value>> field is
> consistent with the choosen type.
>
> for instance
> <<value>> field | choosen type true boolean
> It's ok!
> Today Integer It isn't ok because ''today'' is
> not a integer
>
> To do that, I've tried this (to convert from String to Integer)
>
> check: self.valor.asInteger().isKindOf(Integer)
>
> when <<value>> is an Integer, this works well but it isn't this piece
> of code raises an exception and I couldn't find a way to handling
> exception in eol or evl.
>
> I'd appreciate any help.
>
> Thanks in advance!
>
>
|
|
|
Re: [eol][evl]Handling exception in casting types [message #763692 is a reply to message #763419] |
Sat, 10 December 2011 10:29 |
|
Hi Paulo,
Steffen is right: there is no way to check that it's really an integer from pure EVL. In Java you'd normally try to use Integer.valueOf and then check for a NumberFormatException, but we don't have anything for catching exceptions in EVL.
If performance is not an issue (you only have a few of those strings), you could approximate it with a regex. Since it's a String, you could use the matches method:
self.string.matches("[0-9]+")
However, this simple regex would have to be compiled again and again, and it wouldn't take into account all the possible variations for a Java integer literal.
Cheers,
Antonio
|
|
|
Re: [eol][evl]Handling exception in casting types [message #763698 is a reply to message #763692] |
Sat, 10 December 2011 10:55 |
|
Actually, String provides the isInteger() and isReal() built-in operations but it seems that the book hasn't been updated accordingly (will do this shortly). E.g.
"123".isInteger() -> true
"abc".isInteger() -> false
There is no isBoolean() operation but this could be implemented as follows:
operation String isBoolean() : Boolean {
return self == "true" or self=="false";
}
Cheers,
Dimitris
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04373 seconds