use the type system to declare the number of elements in an array

60 views
Skip to first unread message

Víctor R. Escobar

unread,
Oct 15, 2015, 5:11:09 PM10/15/15
to Haxe
Hello!

I wanted to learn a bit more about the haxe type system so I just implemented the Elm example which uses the type either, to do an array partition based on the type either.

I tried to translate everything line to line, so I conclude that the haxe type system actually misses nothing from a complex type system as in Elm's. Except by one detail: in the original Elm code the partition function can specify that the return type is a tuple of two arrays from type A and type B.

// Original Elm Signature:
partition : List (Either a b) -> (List a, List b)

// My Haxe version
static function
partition<A,B>(eithers:EitherArray<A,B>):Array<EitherArray<A,B>>

The problem of my return type is that it doesn't enforce the whole correctness of the function just by using the type system (with a wrong implementation still the groups could be mixed).
How could I do that in haxe? Something like that doesn't even compile:

static function partition<A,B>(eithers:EitherArray<A,B>):Array<[Array<A>,Array<B>]>

If there is a concept or similar functionality could someone point me to it? 
Since the return array has a fixed size, would it be possible a solution based on macros?



Thanks!

Juraj Kirchheim

unread,
Oct 15, 2015, 5:46:29 PM10/15/15
to haxe...@googlegroups.com
What about this?

  static function partition<A, B>(mixed:Array<haxe.ds.Either<A, B>>):{ left: Array<A>, right:Array<B> };

Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Laurence Taylor

unread,
Oct 15, 2015, 7:56:51 PM10/15/15
to haxe...@googlegroups.com
There's one here:

and you could use it like:

    arr.partition(stx.Eithers.isLeft);

Víctor R. Escobar

unread,
Oct 16, 2015, 3:02:25 PM10/16/15
to Haxe
Thank you for the answers,

I am impressed by the answer pointing 0b1kn00b/stx_addendum... for a second I though it was an alternative implementation of the Std... but it actually looks to be very interesting even by not having test cases.

About the partition implementation looks much clear the type than with my solution (but it needs a dependency on tink) ... I have to try it and check whether it generates some output code or it is just a helper to make more enjoyable the programming with complex types.

Thanks for the sharing!
Reply all
Reply to author
Forward
0 new messages