Check if a variable is convertible to a string in Hacklang

1.5k views Asked by At

I have a Hacklang generic class like this:

class SomeClass<T> {
    public function __construct(private T $input) {

    }

    public function __toString() : string {
        return (string)$this->input;
    }
}

I do not want to limit the variable that can be used as <T>, but I do want it to be convertible to a string.

Is there a way in Hacklang to specify a given variable should be bool, int, float, string or an object with __toString?

1

There are 1 answers

2
Janos Pasztor On BEST ANSWER

After doing a fair bit of digging, there is no solution to this in Hacklang at the moment. The only thing you have is \Stringish, an undocumented interface that covers both native strings and objects with the __toString method. It does not, however, cover int, float or bool.

Source: interfaces.hhi