# How to use Constructible that requires certain fields?

**URL:** <https://community.haxe.org/t/how-to-use-constructible-that-requires-certain-fields/3910>\
**Category:** Haxe\
**Created:** [March 23, 2023, 2:40pm UTC](https://community.haxe.org/t/how-to-use-constructible-that-requires-certain-fields/3910 "2023-03-23T14:40:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![distantforest](https://avatars.discourse-cdn.com/v2/letter/d/58956e/32.png) [@distantforest](https://community.haxe.org/u/distantforest)\
**Post date:** [March 23, 2023, 2:40pm UTC](https://community.haxe.org/t/how-to-use-constructible-that-requires-certain-fields/3910/1 "2023-03-23T14:40:38Z")

</div>

Hi I’m trying to use constructibles to instantiate a class in a callback. This constructible needs to have certain fields.

Here is an example of what I mean:

```haxe
import haxe.Constraints;

// This is from an external library (deepnightlibs)
class ExternalClass<T:Recyclable> {
	public function new(valueConstructor:Void->T) {}
}

private typedef Recyclable = {
	function recycle():Void;
}

class Test {
	static function main() {
		testFunc(TestClass);
	}

	@:generic
	static function testFunc<T:Constructible<Void->Void>>(cls:Class<T>):T {
		// >> It errors here: "testFunc.T should be { recycle : () -> Void }" <<
		return new ExternalClass(() -> new T());
	}
}

class TestClass {
	public function new() {}

	public function recycle() {}
}

```

However there’s an issue, the callback requires that the constructible has certain fields (in this case `recycle()`.

Pretty much `ExternalClass` requires a callback that returns a `T:Recyclable`. How would I do this with constructibles?

Here is the haxe playground of the above code and the error:

> **[Try Haxe !](https://try.haxe.org/#e1A19F38)**
>
> Haxe is an amazing multiplatform programming language. Check it out !

---

<div class="post-metadata">

**Author:** ![distantforest](https://avatars.discourse-cdn.com/v2/letter/d/58956e/32.png) [@distantforest](https://community.haxe.org/u/distantforest)\
**Post date:** [March 24, 2023, 12:40am UTC](https://community.haxe.org/t/how-to-use-constructible-that-requires-certain-fields/3910/2 "2023-03-24T00:40:48Z")

</div>

Managed to get a solution with a help of a certain chat ai program 🤓

You can get a Constructible that requires certain fields by using intersections (the `&` operator):

`testFunc<T:Constructible<Void->Void> & Recyclable>(cls:Class<T>)`

Using the **`&`** to merge the two types Constructible & Recyclable.

Full example:

```haxe
import haxe.Constraints;

typedef Recyclable = {
	function recycle():Void;
}

class Test {
	static function main() {
		testFunc(TestClass);
	}

	@:generic
	static function testFunc<T:Constructible<Void->Void> & Recyclable>(cls:Class<T>) {
		return new ExternalClass(() -> new T());
	}
}

class TestClass {
	public function new() {}

	public function recycle() {}
}

class ExternalClass<T:Recyclable> {
	public function new(valueConstructor:Void->T) {}
}

```

Here is a working example.

> **[Try Haxe !](https://try.haxe.org/#23461C86)**
>
> Haxe is an amazing multiplatform programming language. Check it out !
