-
Notifications
You must be signed in to change notification settings - Fork 20
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
Redefine some top-level operators to allow for overloading #531
Comments
Just a note, the code you posted doesn't work, it fails with:
But you can make it work by moving the overload to the type definition and adding another overload to make the overload resolution ambiguous:
Now your code works, you can even do something like:
And extend it:
|
The above code is more or less the technique used by F# libraries like FSharpPlus, Fleece (replicated later in Chiron), FSharpGPU and possible others that I don't know yet. We can either add code like that in libraries or as you suggest in F# core. The difference is that if it's added in F# core it will be done with simulated members, as it is now with all the math operators. In both cases the main problem is to come up with a default definition for existing types, since they can't be added later in a natural way. So doing it in libraries give us some more flexibility, since implementing changes in F# core is a complicated and long process, given the importance of the core lib. Regarding breaking existing code, with the above code if you define a function like:
It will no longer compile:
But if it is done with simulated members, a default can be added which would solve this scenario, still I think there might be some specific scenarios, in presence of inline functions, where type annotations will be required. So I'm not sure if it's good to add them in F# core, it sounds interesting but we'll need an extensive analysis and discussion about the simulated members for existing types. |
I also had the "second overload" code at hand but, strangely, both compile fine for me. In any event both show the same principle. Using simulated members sounds like the right approach but then it's no longer simple. I understand your point. |
I tried it with VS2015.
What's your platform? Maybe we should report the bug. |
We're not specifically going to do this change to FSharp.Core, as it would almost certainly be a breaking change. |
Many operators have standard global definitions that make them unsuitable for operator overloading, e.g. ! & @ %
Redefining them is not an option because you lose the standard behavior and that would obviously break existing code.
But sometimes they can be redefined in such a way that the standard behavior remains unaffected while allowing for operator overloading in your classes.
For example, the ! operator could be redefined like this:
This still works in the same way for reference types, but you can overload it for a custom class:
Pros and Cons
The advantages of making this adjustment is that some existing operators become overloadable (operator ! in the example).
The disadvantages of making this adjustment to F# are not obvious at this moment. If all the test cases for reference types pass then we could say it has no downsides.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: