HaxeFoundation / haxe

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

HTML externs, improve type unions #7596

Open haxiomic opened 5 years ago

haxiomic commented 5 years ago

If a type union has nested EitherTypes then we can have unification issues, for example (from @SlavaRa's PR #7593)

typedef BlobPart = Array<
    haxe.extern.EitherType<
        haxe.extern.EitherType<
            ArrayBufferView,
            ArrayBuffer
        >,
        haxe.extern.EitherType<Blob,String>
    >
>;

var arrayBuffers = [new ArrayBuffer()];
var x: Array<BlobPart> = arrayBuffers;

To fix we can create an abstract for each type union that merges all EitherTypes:

abstract BlobPart(Dynamic)
    from ArrayBufferView to ArrayBufferView
    from ArrayBuffer to ArrayBuffer
    from Blob to Blob
    from String to String
{}
haxiomic commented 5 years ago

To work around in the short term, this fails:

var arrayBuffers = [new ArrayBuffer()];
var x: Array<BlobPart> = arrayBuffers;

but this works:

var x: Array<BlobPart> = [new ArrayBuffer()];

http://try-haxe.mrcdk.com/#99968

nadako commented 5 years ago

Is it possible to have this for 4.0? People are having issues with their codebases like #7593.

haxiomic commented 5 years ago

Hey, I've started working on it this evening – it's quite a big change because there's a lot of typedefs in the webidls, all these are currently inlined into EtherTypes so we could end up lots of new files if they're now converted to abstracts

However I can't see a problem with merging the quick-fix PR for @SlavaRa 's issue in the short term