From: Keats <ke...@xa...> - 2004-03-10 16:44:32
|
> > > Another approach might be to change the casting functions to return > > objects > > instead of primitives. E.g., > > Integer toInt(Object o); > > instead of > > int toInt(Object o); > > I like this idea a lot! We use $toInt() and $toLong() everywhere, and > every time we pass in a bad value, we get a stinkin' exception where a > Null would be better. It turns out that I was already returning Object wrapped primitives to this change is trivial. > Might be nice if we also had $toInt($input, $DefaultValue) variations > too, for the cases where $input isn't a valid int and a Null isn't good > enough. > > Something like: > #set $xid = $toInt($Form.XID, -1) This is simple enough. I doesn't even require a configuration change in WebMacro, since it will pick up all versions of a named function in the config. One issue here (related to your next point) is for non-integer conversion you'll need to cast the default value. Eg, #set $age = $toShort($Form.AGE, $toShort(0)) or better, #const $short0 = $toShort(0) #set $age = $toShort($Form.AGE, $short0) I could implement the default argument as a generic Object, but then we'd need two conversions for each call. Hmmm. > As an aside, it would be nice if WM were smart enough to up-cast > Integers to Longs when calling methods on a context object: > > public String passALong(long l) { return l + ""; } > > $Obj.passALong(1) > > as far as I remember, this doesn't work. Ya gotta write this: > $Obj.passALong($toLong(1)) > > which is just annoying b/c you gotta keep up with the argument types of > all your methods. Just knowing that it's a "number" should be good > enough. Yeah I remember looking at this a ways back. I believe we made it work for WMScript expression, but not for method calls. It gets complicated since you can have multiple signature for a method. Plus we don't encourage method calls from templates. Keats |