Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jasononeil
Last active December 21, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasononeil/6257011 to your computer and use it in GitHub Desktop.
Save jasononeil/6257011 to your computer and use it in GitHub Desktop.
An Abstract (A) that is Iterable<T> cannot be automatically cast to an Abstract (B) that has a "@:from" cast for Iterable<T>
class Cast
{
static function main()
{
var array = [0,1,2,3];
var list = Lambda.list( array );
var map = [ "a"=>0, "b"=>1, "c"=>2 ];
var stringMap:haxe.ds.StringMap<Int> = map;
var myAbstract:AbstractIterator<Int> = array;
var myAbstract2:AbstractIterator2<Int> = array;
for( int in array ) { $type(int); } // Int
for( int in list ) { $type(int); } // Int
for( int in stringMap ) { $type(int); } // Int
for( int in map ) { $type(int); } // Int
for( int in myAbstract ) { $type(int); } // Int
for( int in myAbstract2 ) { $type(int); } // Int
var myInt1:MyInt = array;
var myInt2:MyInt = list;
var myInt3:MyInt = stringMap;
// var myInt4:MyInt = map; // "Map<String, Int> should be MyInt"
// var myInt5:MyInt = myAbstract; // "AbstractIterator<Int> should be MyInt"
// var myInt6:MyInt = myAbstract2; // "AbstractIterator2<Int> should be MyInt"
}
}
abstract MyInt(Int) from Int to Int
{
@:from public static function fromIterable( it:Iterable<Int> ):Int {
return Lambda.count( it );
}
}
abstract AbstractIterator<T>(Iterable<T>) from Iterable<T> to Iterable<T>
{
public function iterator() return this.iterator();
}
abstract AbstractIterator2<T>(Iterable<T>) from Iterable<T> to Iterable<T>
{
public function iterator() return this.iterator();
@:to public function toIterable():Iterable<T> return this;
}
@jasononeil
Copy link
Author

Cast.hx:9: characters 9-25 : Warning : Iterator
Cast.hx:10: characters 9-24 : Warning : Iterator
Cast.hx:11: characters 9-23 : Warning : Iterator
Cast.hx:15: characters 2-25 : Map<String, Int> should be MyInt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment