-
Notifications
You must be signed in to change notification settings - Fork 21
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
Make user-specified type vars to be skolem #924
Comments
It's a warning, because it is just as likely that the user meant to write The easy way to do what you suggest is to just add If we were to change this design it would be a breaking change, as existing, valid code where that warning is ignored will stop to compile. An alternative suggestion would be to allow negative generic constraints, something like (edit: I see the other suggestion you made is basically the same thing, where I suggested the same thing, as you can already do what you want. Perhaps merge both issues into one, they seem the same) |
I think the current behaviour is right: // You are telling the compiler that cast has the form 'a -> 'b,
// and it does, so this compiles:
let cast : 'a -> 'b = fun x -> x
// You are telling the compiler that for any 'a,'b, cast is defined as follows,
// which is incorrect, so this gives an error:
let cast<'a,'b> : 'a -> 'b = fun x -> x |
Thank you for the suggestion but I don't think this is something we'll pursue, per replies above |
Make user-specified type vars to be skolem
TL;DR: code like
becomes an error, not a warning.
I propose we make universally quantified type variables to be rigid ones.
The existing way of approaching this problem in F# is better explained by compiling this, rather dumb and obvious, example:
The
cast
type means "for all types'a
and'b
, convert'a
to'b
".dotnet fsi example.fs
producesIf you comment out
str
definition, thedotnet fsi
will tell you, that cast has type'a -> 'a
, which is not what user meant initially.As you see, despite using different type variables, the compiler merges them into one. This happens because the variables are not skolem (or rigid) ones.
The negative consequence of current state of arts is that instead of getting an error right in
cast
, we get only warning. And if user callscast
somewhere, compiler will silently replace its type with narrower one, producing type errors - elsewhere. If there are no type annotations and the stars align, the type error might not even be wherecast
is used (in sufficiently polymorphic code), making path back tocast
to be long.When I have errors, I try to fix them first - and only then go to warnings. But this behavior leads to warning on the cause of the problem and type errors on the consequences, which is a priority inversion.
Pros and Cons
The advantages of making this adjustment to F# are
cast
stop typechecking, and do not cause further confusion.The disadvantages of making this adjustment to F# are
warning FS0064
stops compiling.Extra information
Estimated cost (XS, S, M, L, XL, XXL): S-M.
Related suggestions:
let
-declarations more. #839Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: