Re: object(x) rethink
- Posted by Bill Reed <rpfproeu at ai?.?om> Mar 02, 2008
- 756 views
Igor Kachan wrote: > > Bill Reed wrote: > > > > > object useless???? > > > > ---------------------------------------------------------------------- > > A variable must be declared as an object in order to determine what > > type it is. > > > > object x > > > > if sequence(x) then > > puts(1, "variable is a sequence") > > end if > > > > if integer(x) > > puts(1, "variable is an integer") > > end if > > > > ---------------------------------------------- > > doeumentation for gets() from library.doc > > ---------------------------------------------- > > > > > > Example 1: > > > > sequence buffer > > object line > > integer fn > > > > -- read a text file into a sequence > > fn = open("myfile.txt", "r") > > if fn = -1 then > > puts(1, "Couldn't open myfile.txt\n") > > abort(1) > > end if > > > > buffer = {} > > while 1 do > > line = gets(fn) > > if atom(line) then > > exit -- -1 is returned at end of file > > end if > > buffer = append(buffer, line) > > end while > > Hi Bill, > > Try please: > }}} <eucode> > integer i i= 1 > atom a a= 2 > sequence s s={3} > object x x={4,{5}} > > if object(i) then ? i end if > if object(a) then ? a end if > if object(s) then ? s end if > if object(x) then ? x end if > </eucode> {{{ > > Is *object()*, not *object*, useful? > > This *object()* is just a dummy function now. > It stands just for some 'symmetry' with integer(), > atom() and sequence() functions. > > Do you see now? > > Regards, > Igor Kachan > kinz at peterlink.ru Sure, integer, atom, and sequences are *objects*, but not all objects are sequences, and not all objects are atoms and not all objects are integers. I don't check for "if object(x)" or "if not object(x)" If I declare an object as a sequence, and one time it becomes an an integer, I get a "type_check failure". I can assign anything to an object, but I can't assign an integer to a sequence.
object x x = "abc" -- ok x = -1 -- ok But: sequence x x = "abc" -- ok x = -1 -- type_check error, x is -1 To fix: object o sequence s integer i atom a o = "abc" o = -1 o = 5.3 if sequence(o) then s = o elsif integer(o) then i = o elsif atom(o) then a = o end if
Bill