Fsharp: Improve error reporting: missing cast operator for subtype case

Created on 28 Apr 2016  Â·  6Comments  Â·  Source: dotnet/fsharp

Related to #1103 #1127

What

let foo () : IDictionary<'a,'b> = Dictionary<'a,'b>()

Error: This expression was expected to have type IDictionary<'a,'b> but here has type Dictionary<'a,'b>.

Why

New commers don't know about need for upcast, nor they know the idiomatic

let foo () : IDictionary<'a,'b> = Dictionary<'a,'b>() :> _

How

Error: This expression was expected to have type IDictionary<'a,'b> but here has type Dictionary<'a,'b>, consider using cast operator :> _.

Feature Improvement

Most helpful comment

maybe we should suggest to use upcast:

let foo () : IDictionary<'a,'b> = upcast Dictionary<'a,'b>()

All 6 comments

C# people can easily have problems here, because this works in CSharp:

public static IDictionary<object, object> TestM() {
    return new Dictionary<object, object>();
}

I'm not sure if :>_ is really idiomatic here. I personally prefer to
suggest the concrete cast to IDictionary and think newcomers will
understand that better.

Anyways are you trying to implement this? If yes I can probably help if you
have specific questions.
On Apr 28, 2016 6:28 AM, "Isak Sky" [email protected] wrote:

C# people can easily have problems here, because this works in CSharp:

[image: image]
https://cloud.githubusercontent.com/assets/607986/14875359/46000396-0cc7-11e6-93ee-4c3cdc485981.png

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/Microsoft/visualfsharp/issues/1128#issuecomment-215307297

@forki, thanks a lot for your inspiring article / first code push on this effort for better error messages.

I'm not sure if :>_ is really idiomatic here.

If you do C# or VB.NET no :smile: but I think it is idiomatic F#: usage of _ to let compiler infer type / type parameters (especially in this case where it avoids repeating explicit type) is something I'd like to have seen / understood earlier.

Anyways are you trying to implement this? If yes I can probably help if you
have specific questions.

Eventually, but I'm thinking of a easier one (#1129) if I'd ever get started contributing to the compiler.

maybe we should suggest to use upcast:

let foo () : IDictionary<'a,'b> = upcast Dictionary<'a,'b>()

I actually never knew about the :> _ syntax until a few months ago - now I know it, I use it all the time - I think this is a situation were we could be a bit more proactive and give it to them as least as an option in the error message. I also think that we should add the "why" here again e.g. this works in C#, why not in F#. Maybe: -

Error: This expression was expected to have type IDictionary<'a,'b> but here has type Dictionary<'a,'b>. Implicit upcasts are not permitted in this case. Consider either trying to use a flexible type, or explicitly upcasting this using the upcast operator e.g. :> IDictionary<'a, 'b>, or through type inference e.g. :> _.

This is probably a bit too wordy for some - but gets a bit more useful information in.

mhm would work for me.

Was this page helpful?
0 / 5 - 0 ratings