# Having trouble with an abstract with type parameters over an extern class

**URL:** <https://community.haxe.org/t/having-trouble-with-an-abstract-with-type-parameters-over-an-extern-class/2252>\
**Category:** Haxe\
**Tags:** haxe-cs\
**Created:** [February 1, 2020, 12:01am UTC](https://community.haxe.org/t/having-trouble-with-an-abstract-with-type-parameters-over-an-extern-class/2252 "2020-02-01T00:01:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theJenix](https://community.haxe.org/user_avatar/community.haxe.org/thejenix/32/1138_2.png) [@theJenix](https://community.haxe.org/u/theJenix)\
**Post date:** [February 1, 2020, 12:01am UTC](https://community.haxe.org/t/having-trouble-with-an-abstract-with-type-parameters-over-an-extern-class/2252/1 "2020-02-01T00:01:14Z")

</div>

I think this may be a bug, but I wanted to post here first to see if I was maybe doing something wrong. I am trying to define an abstract over an extern class, both with type parameters. What I’m really trying to do is use the abstract to declare higher order operations over an extern class, so I only need to implement a core set of functionality in my targets. Here’s some code that shows the issue:

```haxe
extern class Object_<T> {
    public function method<TN>(cb:T->TN):Object_<TN>;
}

abstract Broken<T>(Object_<T>) {

    inline private function new(thiz:Object_<T>) {
        this = thiz;
    }

    public function method<TN>(cb:T->TN):Broken<TN> {
        return new Broken<TN>(this.method(cb));
    }
}
class TestCase {

    public function broken(obj:Broken<String>):Broken<Int> {
        return obj.method(s -> 1);
    }
}

```

When compiled to C# (using Haxe 4.0.5 and hxcs 4.0.0-alpha, and the command line "haxe -cs dist File.hx), the code generated for TestCase fails to compile. Specifically, it generates this code:

```haxe
 public virtual global::src.Object_<int> broken(global::src.Object_<string> obj) {
            #line 30 "/mnt/c/Users/jesse/Development/bommer-common/bugs/src/GenericAbstract.hx"
            return ((global::src.Object_<int>) (global::src._GenericAbstract.Broken_Impl_.method<object, string>(((global::src.Object_<string>) (obj) ), ((global::haxe.lang.Function) (( (( global::src.TestCase_broken_30 __Fun.__ hx_current != null )) ? (global::src.TestCase_broken_30 __Fun.__ hx_current) : (global::src.TestCase_broken_30 __Fun.__ hx_current = ((global::src.TestCase_broken_30__Fun) (new global::src.TestCase_broken_30__Fun()) )) )) ))) );
        }

```

And fails because of that typecast to Object\_. The code for method is:

```haxe
  public static global::src.Object_<TN> method<TN, T>(global::src.Object_<T> this1, global::haxe.lang.Function cb) {
            #line 16 "/mnt/c/Users/jesse/Development/bommer-common/bugs/src/GenericAbstract.hx"
            global::src.Object_<TN> this2 = this1.method<TN>(((global::haxe.lang.Function) (cb) ));
            #line 16 "/mnt/c/Users/jesse/Development/bommer-common/bugs/src/GenericAbstract.hx"
            return ((global::src.Object_<TN>) (this2) );
        }

```

…because the TestCase code above specifies object as TN, method returns an `Object_<object>` instead of an `Object_<int>`.

I have noticed that if I @:forward(method) and remove it from Broken, it generates compilable code in TestCase. But that would defeat my reason for using an abstract (as stated above).  
Am I doing something silly here? Or is this a bug in the Haxe/C# compiler?

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [February 1, 2020, 4:26am UTC](https://community.haxe.org/t/having-trouble-with-an-abstract-with-type-parameters-over-an-extern-class/2252/2 "2020-02-01T04:26:19Z")

</div>

Try adding `@:nativeGen` meta to your abstract. If that won’t help, then submit an issue.

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [February 1, 2020, 4:29am UTC](https://community.haxe.org/t/having-trouble-with-an-abstract-with-type-parameters-over-an-extern-class/2252/3 "2020-02-01T04:29:49Z")

</div>

You can also try compilation flags like `-D erase-generics` and `-D fast-cast` if they suite your needs. You can find them in `haxe --help-defines`
