What is the Box<T> declaration for?

Asked

Viewed 176 times

2

I was taking a look at documentation of the Hack.

I know that this language is a modification of PHP to introduce type checking.

I saw that they put this example to demonstrate the use of classes, I think it has to do with defining primitive types.

class Box<T> {
  protected T $data;

  public function __construct(T $data) {
    $this->data = $data;
  }

  public function getData(): T {
    return $this->data;
  }
}

I’m used to PHP classes and had never seen such a statement before.

It seems he only accepts the letter T, because I did some tests here on my machine with other names, but it generated errors.

After all, what does this statement mean Box<T>?

  • 2

    Do you want to know about the Box or the <T> in general?

  • The <T>. The Box I already know it’s a class :

  • Did the answer solve what you were looking to know? Do you think you can accept it now? If not, you need something else to be improved?

2 answers

2


This is about generic programming, key in statically typed languages. That’s why it has in Hack and not in PHP.

There the T works as a super variable and will have a value replaced later - this value can only be a data type. When you are instantiating this class you will have to inform about what kind of data the instance will work, after all in statically typed language all data needs a fixed type. This class can box data of any kind. Imagine how crazy it would be to create a new class for each type you need.

In a dynamic language it is simpler because it creates a indirect and treats the data in a generic way and solves at runtime, whether it works or not. In static language needs to solve at compile time giving more security and performance.

It is a way to instruct the compiler to use this class in a special implementation at each instance. Then the compiler will generate on its own a new version of the class for each type effectively instantiated in its application. All the places where there’s a T will receive the type to be instantiated.

Instantiate yourself with a int (ex.: new Box<int>) the generated inner class (you don’t need to know about it) will be something like this:

class BoxInt {
    protected int $data;

    public function __construct(int $data) {
        $this->data = $data;
    }

    public function getData(): int {
        return $this->data;
    }
}

I put in the Github for future reference.

You can use whatever identifier you want there, because you can have several in classes that depend on more than one type. But it needs to have coherence of its use in every class. The error may be because spoke change in other places where still had the T, but do not know, this was not posted, I can only reply speculating.

0

In c# this notation is for you to instantiate the same class, but modify the types . It is a generic type statement. For example...

new Box<string>();

and

new Box<int>();

Generate the Box type object for internally $data will be string in the first case and int in the second.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.