Skip to content

Support coercing on a function type #6821

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

Closed
TheSpyder opened this issue Jun 19, 2024 · 6 comments
Closed

Support coercing on a function type #6821

TheSpyder opened this issue Jun 19, 2024 · 6 comments
Labels

Comments

@TheSpyder
Copy link
Contributor

I recently discovered that coercion works for variants where everything is represented by the same primitive type:
https://rescript-lang.org/docs/manual/latest/variant#coercing-variants-to-primitives

This is great, but I have a function that returns one of these variants and I can't coerce that. It seems to work for no-argument functions, but not functions with arguments.
https://rescript-lang.org/try?version=v11.1.1&code=C4TwDgpgBAhgzgSQHbCgXigHygAXgCgEYBKKAeSWmzznwCZSAVAdwHstcCBmJgCwCcIEAFDCANhFStKyYAC5YiFOnKVxkqMFayV+aRB1yAfFACWKYqImpBwAK78ksgKpJTqDPlJoTFEdahbBycUAGVzAHMJXQB9b181dVQtWQAxQl0gxxc3VGMofDtc9BNzYGJLAJSUVLpMyWDZcKQo6Hz8AHIYErMLYiA

The specific example I'm trying to model is array sort, so I'd need it to work on 2-argument functions as well.
https://rescript-lang.org/try?version=v11.1.1&code=C4TwDgpgBAxg9gWzAQwE4EsDOcB2UC8AUFFAD5QACymAFALQCMAlFADISaYAqAFsjsTKVqNAAwsAogEcArsgA2g8lVrMoAcVQRkwCKl79ChOJDwApTADoAgqlTIQAJkLyIwKNlTv8UGmlQANFAAfgBmAFy+AOTIQTEs+AB8sIgoGNg4CYmCnsAAkjgACvLIMBAA6ujAPH52dImY8uhlAGKoiGJMQTShUOHJ6DjATExAA

@cristianoc
Copy link
Collaborator

One observation: the coercion in the example seems to work if the argument type is not polymorphic, e.g. float.
Coercion was not modified for function types, it still uses the original implementation, but the way it's been implemented for variants might interfere with the pre-existing mechanism in certain cases (e.g. polymorphic argument).

@TheSpyder
Copy link
Contributor Author

Ah, that makes sense. Unfortunately the way I'm trying to use it with Array sort requires polymorphism.

For now I've just used an obj magic.

@cristianoc cristianoc added the bug label Jun 19, 2024
cristianoc added a commit that referenced this issue Jun 25, 2024
See #6821

Free variables are not allowed in coercion, and if that happens, simple unification is performed without ever attempting coercion. This seems to go back a very long time.

There's  probably a good reason why free vars were not allowed. At the same time, there are no objects/classes supported in the language anymore, so it's not clear how those reasons would adapt.

This just marks the place where one could investigate.
cristianoc added a commit that referenced this issue Jul 2, 2024
See #6821

Free variables are not allowed in coercion, and if that happens, simple unification is performed without ever attempting coercion. This seems to go back a very long time.

There's  probably a good reason why free vars were not allowed. At the same time, there are no objects/classes supported in the language anymore, so it's not clear how those reasons would adapt.

This just marks the place where one could investigate.
cristianoc added a commit that referenced this issue Jul 2, 2024
See #6821

Free variables are not allowed in coercion, and if that happens, simple unification is performed without ever attempting coercion. This seems to go back a very long time.

There's  probably a good reason why free vars were not allowed. At the same time, there are no objects/classes supported in the language anymore, so it's not clear how those reasons would adapt.

This just marks the place where one could investigate.
@cristianoc
Copy link
Collaborator

@TheSpyder any more tests for #6828 ?

@TheSpyder
Copy link
Contributor Author

@cristianoc the second example is the one I pulled from my codebase, I haven't come across any others (although I haven't really been looking). I appreciate you looking at this!

@cristianoc
Copy link
Collaborator

@cristianoc the second example is the one I pulled from my codebase, I haven't come across any others (although I haven't really been looking). I appreciate you looking at this!

Great, that example is covered already.

cristianoc added a commit that referenced this issue Jul 3, 2024
* Test: allow free vars in types for coercion.

See #6821

Free variables are not allowed in coercion, and if that happens, simple unification is performed without ever attempting coercion. This seems to go back a very long time.

There's  probably a good reason why free vars were not allowed. At the same time, there are no objects/classes supported in the language anymore, so it's not clear how those reasons would adapt.

This just marks the place where one could investigate.

* Add tests of type coercion with free vars.

* Clean up code for coercion and free variables.

* Update CHANGELOG.md

* Remove unused code for self coercion.

* Remove unused error that suggests double coercion.
@cristianoc
Copy link
Collaborator

Done in #6828

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants