Pergunta

What is correct answer for hack tutorial exercice 16?
Link to tutorial: Hacklang tutorial

My modified code (not marked as solution):

<?hh
// The type 'this' always points to the most derived type
class MyBaseClass {
  protected int $count = 0;

  public function add1(): this {
    $this->count += 1;
    return $this;
  }
}

class MyDerivedClass extends MyBaseClass {
  public function print_count(): void { echo $this->count; }
}

function test(): void {
  $x = new MyDerivedClass();
  $x->add1()->print_count();
}

I replaced MyBaseClass by this but still not marked as correct (green text with Exercice number) .. what is correct answer?

Foi útil?

Solução

I'm an engineer working on Hack. I'm pretty sure we have a bug in our completion detection logic for this exercise in the tutorial. Your code looks correct to me -- changing the return type to this is, as far as I can see, all you were supposed to do. I'll follow up and get this bug fixed. Sorry about that!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top