Ex09: Instantiation of a inherited class

Osterlaus
BSc Spammer
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

Ex09: Instantiation of a inherited class

Beitrag von Osterlaus »

Hi there,
thanks for publishing the current exercise, which is once more fun, but tricky to solve. So, I have a question about the following testcase:

Code: Alles auswählen

  test("does not override field in child") {
    testclasses = Map(
      'Foo -> Class('Object, List('pField), Map.empty),
      'Bar -> Class('Foo, List('pField, 'cField), Map.empty))
    val result = eval(
      GetField(New('Bar, newAltDummy, newDummy, newAltDummy), 'cField))
    assert(result === altDummyVal)
  }
We instantiate the class Bar with three variables, even if that class only knows about two of them. The testcase leeds me to the thought that they should be assigned from right to left here, such that Bar knows about the two last ones and its superclass Foo is instantiated with the remaining ones - is that correct?

Benutzeravatar
cofi
Mausschubser
Mausschubser
Beiträge: 86
Registriert: 22. Sep 2009 12:07

Re: Ex09: Instantiation of a inherited class

Beitrag von cofi »

Practically, you are correct. Technically, the layout is

Code: Alles auswählen

<superclassArgument*><classArgument*>
in left-to-right order.

Osterlaus
BSc Spammer
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

Re: Ex09: Instantiation of a inherited class

Beitrag von Osterlaus »

Thanks, that helped :)

But I got another question. When I directly want to access a field, like in the testcase "method call with direct field access", the interpreter currently have no information about the scope it should search in. The counterpart is Set(id, expr) -- in which scope should that be done? Or should we handle this within Call() where we know about the scope?

Benutzeravatar
cofi
Mausschubser
Mausschubser
Beiträge: 86
Registriert: 22. Sep 2009 12:07

Re: Ex09: Instantiation of a inherited class

Beitrag von cofi »

Read the task again
Allow fields to be used directly by their names in methods (so there is no need to use GetField('this, …) or SetField('this, …) to access a field)
Should make it obvious ;)

Osterlaus
BSc Spammer
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

Re: Ex09: Instantiation of a inherited class

Beitrag von Osterlaus »

Hmm, yeah, for some tasks that helps. But within the full test szenarios, the interpreter also accesses them within the method body... so, I need to add *all* current values of an object to the env...???

Benutzeravatar
cofi
Mausschubser
Mausschubser
Beiträge: 86
Registriert: 22. Sep 2009 12:07

Re: Ex09: Instantiation of a inherited class

Beitrag von cofi »

All fields, yes.

xmanu
Windoof-User
Windoof-User
Beiträge: 30
Registriert: 30. Apr 2009 12:08

Re: Ex09: Instantiation of a inherited class

Beitrag von xmanu »

Osterlaus hat geschrieben:Hi there,
thanks for publishing the current exercise, which is once more fun, but tricky to solve. So, I have a question about the following testcase:

Code: Alles auswählen

  test("does not override field in child") {
    testclasses = Map(
      'Foo -> Class('Object, List('pField), Map.empty),
      'Bar -> Class('Foo, List('pField, 'cField), Map.empty))
    val result = eval(
      GetField(New('Bar, newAltDummy, newDummy, newAltDummy), 'cField))
    assert(result === altDummyVal)
  }
I have another question for the same testcase. So as we now know the first argument passed in (newAltDummy is the value for 'pField in 'Foo, newDummy is the value for 'pField in 'Bar), why shouldn't it override the value? Isn't that just a convention? Why is there the need to pass over the value for both anyways? Shouldn't it be better to just pass in the 2 parameters for 'Bar, because the first one is shadowing 'Foo's Parameter?

f_jakob
Mausschubser
Mausschubser
Beiträge: 50
Registriert: 27. Okt 2009 14:05

Re: Ex09: Instantiation of a inherited class

Beitrag von f_jakob »

The reason behind the testcase is the behavior of the following Java code:

Code: Alles auswählen

class Foo {
	int x = 3;
}

class Bar extends Foo {
	int x = 7;
	
	public static void main(String[] args) {
		Bar bar = new Bar();
		System.out.println("Bar: " + bar.x);
		System.out.println("Foo: " + ((Foo) bar).x);		
	}
}
The output will be:

Code: Alles auswählen

Bar: 7
Foo: 3
As you can see, the fields are not overridden. We try to mimic this behavior with our interpreter, even if we do not have the means to type cast or otherwise specify, which field we actually want to refer to.

Antworten

Zurück zu „Archiv“