Re: RE: pass by reference
- Posted by Derek Parnell <ddparnell at bigpond.com> Feb 19, 2002
- 470 views
20/02/2002 1:23:04 PM, bensler at mail.com wrote: > >I was one of the people who suggested pass by referencing. > >Here's my thoughts. >Why on EARTH do I want to type check my variables only to make my >program crash 'elegantly', albeit better than the current alternative(no >type checks). >With reference passing, I could type check my variable, and when it's >out of bounds, I could modify it, or set it to a default value. >IMHO, if I'm going to sacrifice some speed to use typechecking in my >program, it should be to safeguard my program, not just give a little >bit more useful error info. > >You COULD do it with pointers(shudder), but that is a lot of places >where you might have to add allocations, deallocations, peeks and pokes. >It also means you would have to do double typechecking. First using a >generic typecast, and then a secondary typecheck within the routine. > Chris, from this description is sounds like the problems you are trying to solve are: (1) Sometimes, variables are used without being initialized. (2) Sometimes, variables are used that contain out-of-bounds data. (3) Sometimes, variables are used that are of the wrong data type. (4) Sometimes, sequence are used that have the wrong number, or type, of elements. and you speculate that the type checking mechanism might be a convenient way to resolve these, so long as we can modify the variable being typechecked, inside the typecheck routine itself. Have I understood you correctly? If so, I have also want this type of functionality. It shouldn't add too much overhead to the interpreter as it would only be used where user-defined type checking is being used. And as is would be restricted to only the variable being type checked, which is when the variable is being assigned to anyway, it wouldn't add any unexpected side-effects. The other thing you might want to add is the ability to detect at run time, whether or not a variable has been initialized. I can imagine something like: ------------- type TextLine(object x) if not initialized(x) then x = {} elsif atom(x) then if x = -1 then -- Mark EOF with an empty sequence. x = {} else crash_message("TextLine must be a string or -1") return 0 end if else for i = 1 to length(x) do if sequence(x[i]) then crash_message("TextLine must not have substrings") return 0 elsif not integer(x[i]) then crash_message("TextLine must only contain integers") return 0 elsif x[i] < 0 or x[i] > 255 then crash_message("TextLine must only contain bytes") return 0 end if end for end if return 1 end type TextLine inputdata inputdata = gets(f) while length(inputdata) > 0 do . . . inputdata = gets(f) end while ---------- cheers, Derek. --------- Cheers, Derek Parnell