Re: strong typing and error handling

new topic     » goto parent     » topic index » view thread      » older message » newer message

Until such time as Euphoria has pass-by-reference, there is a nasty
workaround for types that will only be used for a few variables--its
impractical for programs with hundereds of variables. Let's assume we want
to have variables that will reject a sequence, but modify any atom to coerce
its value to an integer between 1 and 10.

-- this code needs to be written only once
function coerce(object x)
    if atom(x) then
        x=floor(x)
        if x<min then x=1 end if
        if x>max then x=10 end if
    end if
    return x
end function

-- the code from here on is repeated for EACH variable
integer SET_A

type a_object(object x)
   object y
   y=coerce(x)
   if not equal(x,y) then call_proc(set_a,{y}) end if
   return integer(x)
end type

a_object a

procedure set_a(object x)
    a=x
end procedure

SET_A=routine_id("set_a")


This is a huge amount of overhead to simulate pass by reference, but it does
work.  I wish Rob would relent on pass by refernece for types at
least --preferably for all routines.  Wouldn't we all prefer to code the
above as:

type int1to10(by_ref object x)
    if sequence(x) then return 0 end if
    x=floor(x)
    if x<1 then x=10 end if
    if x>10 then x=10 end if
    return 1
end type

int1to10 a,b,c,d,e

For the OpenEU team, I would suggest two keywords that would  be allowable
only before a type specifier in a parameter list: by_ref and by_val (by_val
for documentation: pass by value would and should be the default -- VB
defaulting to ByRef is really, really bad).

-- Mike Nelson

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu