munus icon indicating copy to clipboard operation
munus copied to clipboard

Tuple and generic types

Open akondas opened this issue 4 years ago • 2 comments

Question about Tuple generics. There are two options:

  1. Currently implemented: there is no support for generics in Tuple

  2. Implemented in Vavr way:

Tuple1<T1>
Tuple2<T1,T2>
Tuple3<T1,T2,T3>
...
Tuple8<T1,...,T8>

I'm not convinced with second option ... :thinking:

akondas avatar Dec 14 '19 21:12 akondas

I had some thought about tuples. The first time when I saw how it was done in vavr, I was not convinced too, but then thought occurred to me. Apart from the fact that there are available generics on the tuple on the second option, I think there is one more situation when multiple tuples may help.
In this case below, someone may need to make a double check on Tuple:

class A {
    public function foo(Tuple $tuple)
    {
        return matchValue(true)->of(
                caseCall($tuple->arity() === 2, fn () => $this->processTuple2($tuple)),
                caseCall($tuple->arity() === 3, fn () => $this->processTuple3($tuple))
            );
    }
    
    public function processTuple2(Tuple $tuple)
    {
        // someone may use this omitting foo method
        if ($tuple->arity() !== 2) {
            throw new Exception(// ...);
        }
        // ...
    }
    
    public function processTuple3(Tuple $tuple)
    {
        // someone may use this omitting foo method
        if ($tuple->arity() !== 3) {
            throw new Exception(// ...);
        }
        // ...
    }
}

And with Vavr way tuples, this case may look like this:

class A {
    public function foo(Tuple $tuple)
    {
        return matchValue($tuple)->of(
                caseCall(isInstanceOf(Tuple2::class), [$this, 'processTuple2']),
                caseCall(isInstanceOf(Tuple3::class), [$this, 'processTuple3'])
            );
    }
    
    public function processTuple2(Tuple2 $tuple)
    {
        // no need to check it again, because of typing
        // ...
    }
    
    public function processTuple3(Tuple3 $tuple)
    {
        // no need to check it again, because of typing
        // ...
    }
}

What do you think @akondas?

mtk3d avatar Jun 06 '21 22:06 mtk3d

I agree with it. And we have a material for documentation right away :wink:

akondas avatar Jun 07 '21 07:06 akondas