Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typedef and Interface #11026

Open
filt3rek opened this issue Mar 21, 2023 · 4 comments
Open

Typedef and Interface #11026

filt3rek opened this issue Mar 21, 2023 · 4 comments
Milestone

Comments

@filt3rek
Copy link
Contributor

Hej,

I don't know if it's intentional but I just realized that Haxe doesn't complain here : https://try.haxe.org/#729BD642

typedef TFoo = {
	a:Int,
	b:Int
}

interface MyInterface {
	public function get():TFoo;
}

class MyClass implements MyInterface {
	public function new() {}

	public function get()	/*: TFoo*/ {  // <-------------------- 
		return {
			a: 0,
			b: 0,
			c: 0
		}
	}
}

class Test {
	static function main() {
		var cl = new MyClass();
		cl.get();
	}
}

If the return type is uncommented it does complain as it should, but without, it should also complain at least that the return type isn't the same as in the interface right ?

@back2dos
Copy link
Member

The error you're getting is not related to an interface. This fails too:

typedef TFoo = {
	a:Int,
	b:Int
}

class MyClass {
	public function new() {}

	public function get():TFoo {
		return { a: 0, b: 0, c: 0 }
	}
}

The reason is that if you specify the return type, the compiler uses it for top down inference and you have the same type error as ({ a: 0, b: 0, c: 0 } : TFoo). There are multiple discussions about this, most notably this one: #3192

@filt3rek
Copy link
Contributor Author

Hej Juraj,

Thx for your answer.
In fact I don't get any error and that why it's confusing me. I would love Haxe to throw me the errror saying that there is an extra field "c" but it doesn't.
And maybe it's not related to an interface but especially in the case of interface, where I'm wainting all to be strict (an interface is here for that I think), I would Haxe throw me an error, but it doesn't.
That's why I permit to write this issue.

@filt3rek
Copy link
Contributor Author

filt3rek commented Mar 21, 2023

But there is maybe something with variance because if for example the function doesn't return "a", compiler does throw an error as expected. So feel free to close this, I just wanted to point that

@Simn
Copy link
Member

Simn commented Mar 22, 2023

I suppose the question here is if the return type should be typed against the type from the interface, which on first glance sounds like a good idea.

@Simn Simn added this to the Later milestone Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants