Re: strong typing and error handling
- Posted by Derek Parnell <ddparnell at bigpond.com> Jun 27, 2003
- 378 views
On Fri, 27 Jun 2003 07:55:08 +0000 (06/27/03 17:55:08) , Peter Willems <peter at integratedmoves.com> wrote: > > > Maybe I'm shooting myself in the foot here, but.... > > One of the things in Eu that I really like is the rule-like > variable typing that let us set boundaries for a variable. > But it seems to me that the absense of an error-handler makes > this close to useless because when I try to load a value into > a variable that is outside it's boundaries, it exits the app > with an error message. Thus I need to have my app check anyway > when putting values into vars to prevent my app from exiting, > so when I have to check var boundaries myself, what does the > option to set boundaries when defining a var does for me ? > > Or do I mis something and IS it possible to capture boundary > overflow (or underflow) in a simple way so I can write a simpe > error-handler ? > You can almost do this, so long as you don't mind not having access to any meta-data. procedure logerr(sequence msg, object data) printf(2, "Type Failure: %s\n", {msg}) pretty_print(2, data, {3}) end procedure global type Customer(object x) if not sequence(x) then logerr("Customer Type must be a sequence.", x) return 1 -- Return to app end if if length(x) != 5 then logerr("Customer Type must have 5 fields.", x) return 1 -- Return to app end if ...etc... return 1 end type So long as you always return non-zero from a type definition, your application won't crash with a 'type' error. If you don't mind it crashing when an assignment is made you do this... sequence vTypeDefCrash vTypeDefCrash = {0} procedure SetTypeDefCrash( object NewVal) if sequence(NewVal) then if length(vTypeDefCrash) > 1 then vTypeDefCrash = vTypeDefCrash[2..length(vTypeDefCrash)] return end if else vTypeDefCrash = NewVal & vTypeDefCrash end if end procedure global type Customer(object x) if not sequence(x) then logerr("Customer Type must be a sequence.", x) return vTypeDefCrash[1] end if if length(x) != 5 then logerr("Customer Type must have 5 fields.", x) return vTypeDefCrash[1] end if ...etc... return 1 end type So that by default it would crash except when you don't want it to crash during a certain part of you code... SetTypeDefCrash(1) -- Turn on crash avoidance . . . -- critical code SetTypeDefCrash("") -- Restore it to whatever it was. Unfortunately, you can't tell the user what line of code this was on, or the name of the variable, or even what source file it was in. -- cheers, Derek Parnell