HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.15k stars 658 forks source link

Allow arbitrary type parameters on @:genericBuild types #3089

Closed back2dos closed 10 years ago

back2dos commented 10 years ago

Is there any reason why the type parameters must at all be declared beforehand?

Ideally, we could have types like Or<A, B, ..., X> or And<A, B, ..., X> or Minus<T, A, B, ..., C> etc.

Would it make sense to discuss an alternative syntax for @:genericBuild (which does seem a little awkward to put it mildly) or would I just be wasting everyone's time? ;)

Simn commented 10 years ago

I thought about this too in the context of a Tuple type. It should be possible to lift this restriction.

nadako commented 10 years ago

What's the status of this issue?

Simn commented 10 years ago

I implemented it but didn't really like my implementation, so this issue will probably sit here for a while.

Simn commented 10 years ago

The type parameter name has to be Rest for this to work.

back2dos commented 10 years ago

This is awesome! \o/

I would like to suggest for the parameter to be called _ if the parser (or typer?) doesn't take issue with that. Just to avoid adding any more magic identifiers.

On Thu, Jul 17, 2014 at 3:14 PM, Simon Krajewski notifications@github.com wrote:

The type parameter name has to be Rest for this to work.

— Reply to this email directly or view it on GitHub https://github.com/HaxeFoundation/haxe/issues/3089#issuecomment-49304483 .

Simn commented 10 years ago

That would require parser changes, and I would like to reserve the underscore in case we decide to support higher kinded types at some point.

nadako commented 10 years ago

This one gives an error:

Fatal error: exception Assert_failure("type.ml", 474, 9)
Raised at file "type.ml", line 474, characters 9-21
Called from file "type.ml", line 473, characters 42-52
Called from file "type.ml", line 476, characters 13-32
Called from file "typer.ml", line 832, characters 2-35
Called from file "typer.ml", line 3195, characters 15-45
Called from file "typer.ml", line 2517, characters 13-41
Called from file "list.ml", line 55, characters 20-23
Called from file "typer.ml", line 2511, characters 10-523
Called from file "typer.ml", line 2638, characters 11-34
Called from file "typer.ml", line 2652, characters 9-16
Called from file "typer.ml", line 2716, characters 10-38
Called from file "typeload.ml", line 1280, characters 2-25
Called from file "typeload.ml", line 2028, characters 21-72
Called from file "typecore.ml", line 355, characters 3-6
Called from file "typeload.ml", line 2232, characters 51-59
Called from file "typecore.ml", line 345, characters 3-6
Called from file "main.ml", line 1401, characters 2-21
Called from file "main.ml", line 597, characters 3-11
Called from file "main.ml", line 1611, characters 1-35
class Signal<Rest> {
    public function new() {}
}

class Main {
    static function main() {
        var signal = new Signal<Int,String>();
    }
}
Simn commented 10 years ago

Uhm yes, it doesn't actually check for the @:genericBuild metadata right now.

andyli commented 10 years ago

Why is it named Rest, but not Any or Dynamic? Is that it can be used like class Foo<A,B,Rest> to make sure there are >= 3 type params?

nadako commented 10 years ago

Here's a sample Signal class builder using this new feature: https://gist.github.com/nadako/b086569b9fffb759a1b5

waneck commented 10 years ago

whoa, that's awesome!

nadako commented 10 years ago

@andyli yeah, class Foo<A,B,Rest> is supposed to make sure you have >= 2 type params (not 3 tho)

andyli commented 10 years ago

I see, cool :)

ncannasse commented 10 years ago

Nice one, Tuple should be quite easy now

Simn commented 10 years ago

haxe.ds.Tuple? :)

nadako commented 10 years ago

I dunno, IMHO the tuple type, even if implemented in a nicely optimized way (i.e. stack allocated where possible) is not very useful without some short syntax and value unpacking.

Still, I think what we can build with @:genericBuild is very nice, but I'd vote against adding it to std lib right now, because we don't want to be constrained by it later if we decide to implement some syntax-powered tuple type.

And of course I, as many people would vote for adding a properly designed core tuple type to haxe.

nadako commented 10 years ago

Anyway, here's a simple tuple builder: https://gist.github.com/nadako/2ad4246f257e627a5833

PDeveloper commented 8 years ago

Would there be a way to pass generic parameters on to a superclass?

(For example, would it be possible to extend that Tuple implementation?)