[Grace-core] Inheritance and object initialisation

Kim Bruce kim at cs.pomona.edu
Wed Jul 18 15:58:06 PDT 2012


Here are a few thoughts on Andrew's answers.  I'll skip those that are controversial -- and go non-committal on a few.

On Jul 17, 2012, at 5:32 PM, Andrew Black wrote:

> 
>> var tmp := 1
>> def a = object {
>>   def x = tmp
>> }
>> tmp := 2
>> def b = object {
>>   inherits a
>> }
>> Does a.x == b.x? What is b.x?
> 
> 
> I think that we can all agree that a.x == 1.  By the deep copy semantics, b.x == 1 also.   The fact that tmp is changed after a was created doesn't change the value of a.x.  

I had just about decided you were correct (it even matched my semantics definition of subclass semantics!) and my answer of b.x == 2 was wrong, when I decided to run an equivalent Java program:

public class MyTester {
	public int tmp = 1;
	public class InnerSup {
		public int x = tmp;
	}
	
	public class InnerSub extends InnerSup {
	}
	
	public static void main(String[] args) {
		MyTester tester = new MyTester();
		InnerSup isp = tester.new InnerSup();
		tester.tmp++;
		InnerSub isb = tester.new InnerSub();

		System.out.println("tmp = "+tester.tmp);
		System.out.println("isp.x = "+isp.x);
		System.out.println("isb.x = "+isb.x);
	}
}

I used inner classes to get a scope with a "global" variable tmp.  Much to my surprise these are the answers I got:
tmp = 2
isp.x = 1
isb.x = 2

I did something similar with Andrew's next example (copied at the end to not get in the way here) and also got answers that matched my original intuitions, rather than Andrew's answers.  (In that case an extra cause of confusion was an update to the global variable accomplished in the constructor code.)

So, we could just decide Java is wrong and go on with our own analysis.  That may be the correct thing to do, but I'd like to understand better why Java did this.

I'll just leave it for now as a puzzle that we should think about.  Please do let me know if you think my code somehow does not mirror the original grace code (or if you know another language that gives a different answer!).

Kim

P.S.  Here is the other code in Java:

public class MyTester {
	public int tmp = 1;
	public class InnerSup {
		public int x = tmp*2;
		public InnerSup() {
			tmp= tmp+1;
		}
	}
	
	public class InnerSub extends InnerSup {
	}
	
	InnerSup isp = new InnerSup();
	InnerSub isb = new InnerSub();

	public static void main(String[] args) {
		MyTester tester = new MyTester();
		System.out.println("tmp = "+tester.tmp);
		System.out.println("isp.x = "+tester.isp.x);
		System.out.println("isb.x = "+tester.isb.x);
	}

}

The output is:
tmp = 3
isp.x = 2
isb.x = 4

I understand and agree with the value of tmp, but am puzzled by isb.x (even though it conforms to my original intuition!).

I haven't tried the examples with self yet.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailhost.cecs.pdx.edu/mailman/private/grace-core/attachments/20120718/d8b05364/attachment.html>


More information about the Grace-core mailing list