1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/* <license>
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
* For full copyright and license information please refer to doc/license.txt.
* </license>
*/
typedef Cloneable<T> = {
//HAXE: F9 bug, this would better be:
//clone : Void -> T
var clone(default,null) : Void -> T;
}
class Impl /*note, does not explicitly implement or extend anything relating to Cloneable*/
{
public var val : Int;
public function new (value : Int )
{
this.val = value;
}
public function clone() : Impl
{
return new Impl( val );
}
}
class DuckTypedef extends haxe.unit.TestCase
{
public function testClone() {
var a = new Impl(6);
var b = doClone( a );
assertEquals( 6, b.val );
assertEquals( 6, doClone2( b ).val );
}
function doClone<T>( item : Cloneable<T> ) : T
{
return item.clone();
}
function doClone2<T>( item : { var clone(default,null): Void -> T; } ) : T
{
return item.clone();
}
}
|