Assigment 6: TryDeref on Box?

Moderator: Konzepte der Programmiersprachen

johanneslauinger
Erstie
Erstie
Beiträge: 11
Registriert: 5. Dez 2016 23:41

Assigment 6: TryDeref on Box?

Beitrag von johanneslauinger »

Hi,

I see from the tests that TryDeref shall derefence WeakRefV values, if possible. What if TryDeref is used on a Box value? Should it derefence it too, or rather cause an error?

Kind regards,
Johannes

eustro
Neuling
Neuling
Beiträge: 10
Registriert: 17. Okt 2019 19:17

Re: Assigment 6: TryDeref on Box?

Beitrag von eustro »

You mean to call TryDeref directly on a new Box or a reference to a Box instead?
The latter should be possible, since we should be able to construct weak references to any construct of the language (if the reference was a WeakRef in the first place).
This test for example should be possible:

Code: Alles auswählen

  test("WeakRef to NewBox and OpenBox") {
    assertResult(NumV(1)) {
      val (res, _) = interp(Let('a, WeakRef(NewBox(1)), OpenBox(TryDeref('a, 23))))
      res
    }
  }
I noticed that several of the tests do not use WeakRefs at all, so I am not sure what their purpose is. For instance all tests that should test WeakRefs on boxes and boxes that contain WeakRefs do in fact not use WeakRefs (I also looked at the newest template version, am I missing something here?). I made my own tests for that, at least a couple.
I hope there is useful information for you.

Best,
Eugen

eustro
Neuling
Neuling
Beiträge: 10
Registriert: 17. Okt 2019 19:17

Re: Assigment 6: TryDeref on Box?

Beitrag von eustro »

Here another test case where a WeakRef is bound (or rather rebound) to a box via SetId expression:

Code: Alles auswählen

  test("NewBox for WeakRef and OpenBox") {
    assertResult(NumV(0)) {
      val (res, _) = interp(
        Let('b, -47098,
          If0(Seqn(SetId('b, WeakRef(NewBox(0))), OpenBox(TryDeref('b, 23))),
            OpenBox(TryDeref('b, 23)),
            'b)))
      res
    }
  }

johanneslauinger
Erstie
Erstie
Beiträge: 11
Registriert: 5. Dez 2016 23:41

Re: Assigment 6: TryDeref on Box?

Beitrag von johanneslauinger »

Thanks for the answers! With my question I tried to refer to use TryDeref directly on a Box value, in which case it would work exactly like OpenBox. See the test case here:

Code: Alles auswählen

test("TryDeref on a Box value") {
  assertResult(NumV(1)) {
    val (res, _) = interp(Let('a, NewBox(1), TryDeref('a, 23)))                                                                                                    
    res
  }
}
I thought this would maybe make sense because a Box is also a bit like a reference to something, so TryDeref might turn it into the value located in the box. Based on your answer I now assume TryDeref should only dereference WeakRefV values.

eustro
Neuling
Neuling
Beiträge: 10
Registriert: 17. Okt 2019 19:17

Re: Assigment 6: TryDeref on Box?

Beitrag von eustro »

I think the exercise doesn't account for that use case, but good point, I didn't think of that!

You could also use TryDeref on regular references to NumV and Closure as well. You would need to decide if you want to replicate the bahavior of a normal Ref and throw an error, if it does not exist or supply it with a fallback expression to evaluate to.
Another point could be something like TryDeref( WeakrefV(WeakrefV(WeakrefV(...))) ).

I did it the simple way: Throw an error if the expression evaluates to something different than WeakrefV :)

Antworten

Zurück zu „Konzepte der Programmiersprachen“