Path: utzoo!attcan!uunet!lll-winken!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!apple!lsr From: lsr@Apple.COM (Larry Rosenstein) Newsgroups: comp.sys.mac.programmer Subject: Re: Object Pascal Keywords: Object Pascal Message-ID: <493@internal.Apple.COM> Date: 27 Jan 89 17:23:05 GMT References: <7096@spool.cs.wisc.edu> Organization: Advanced Technology Group, Apple Computer Lines: 59 In article <7096@spool.cs.wisc.edu> engber@shorty.cs.wisc.edu (Mike Engber) writes: >In the December '86 issue of MacTutor (also Apple Tech Report #6) Ken >Doyle wrote an articl on object Pascal in which he warns that using the >with statement on objects is dangerous in the same way using a with I don't have the article in front of me, so I don't know what Ken wrote. It could be that his comments were applicable to the MPW Pascal compiler at the time. The 3.0 compiler either handles all these problems correctly, or gives you a compile-time warning. For example if you write: with object1 do begin field1 := NewHandle(109); field2 := NewHandle(108); field3 := NewHandle(107); end; the compiler will put the handle that is object1 in a register and dereference it each time it does the assignment. If you write: with object1 do begin field1 := handle1; field2 := handle2; field3 := handle3; end; then the compiler sticks a pointer to the fields in a register and does the assignments with 3 indexed stores. It can do this because the body of the with statement contains no procedure calls. If you write: object1.field1 := NewHandle(10); object1.field2 := handle2; object1.field3 := NewHandle(20); the compiler will explicitly dereference the object handle after calling NewHandle, but will directly store into the object for the 2nd assignment (since the pointer to the object's fields is still in a register at that point). Both of these examples were ones that I just tried with the standard 3.0 MPW Pascal compiler. >Also, for similar reasons Doyle warns against using an object's field as >a var parameter. The language definition seems to allow it, if it is a >problem then I have the same complaint again. In this case, the MPW compiler gives you an error at compiler time (unsafe use of a handle). The 3.0 compiler also catches the case where you pass a field of an object as a non-VAR parameter, but the size of the paramter is more than 4 bytes. In these cases, the compiler still passes a pointer to the field, which would be as unsafe as a VAR paramters. The MPW Pascal compiler has a compile time switch to turn off these error checks, if you know the procedure you are calling can't compact memory.