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

Improve error reporting: Wrong cast operator (:?> instead of :>) #1127

Closed
Tracked by #1103
isaksky opened this issue Apr 28, 2016 · 5 comments
Closed
Tracked by #1103

Improve error reporting: Wrong cast operator (:?> instead of :>) #1127

isaksky opened this issue Apr 28, 2016 · 5 comments

Comments

@isaksky
Copy link

isaksky commented Apr 28, 2016

Related to #1103

What

image

Why

This error message is problematic for newcomers, especially ones from C#, because:

  1. IDictionary<obj,obj> is compatible with Dictionary<obj, obj>
  2. C# only has one cast operator, and this works in C#:
public static IDictionary<object, object> TestM() {
    return new Dictionary<object, object>();
}

Possible solution

Before resorting to this error message, check if the left hand side can be statically cast to the type on the right, and if so, use a "consider using the :> operator" error message instead.

@forki
Copy link
Contributor

forki commented May 3, 2016

first WIP PR: #1149

@isaacabraham
Copy link
Contributor

@isaksky Just a thought - C# does have two keywords - as (which doesn't exist in F# AFAIK) and (). It's more that in C# the "safe upcast" happens implicitly whereas in F# you need to be explicit - although I can't remember if this is only for co/contravariant conversions.

@isaacabraham
Copy link
Contributor

isaacabraham commented May 3, 2016

In fact here's another whole host of example cases...

type IFoo = abstract member Bar : unit -> unit
type Foo() = interface IFoo with member __.Bar () = ()

let Thing : IFoo = Foo() // fails
let Thing2 : IFoo = Foo() :?> IFoo // fails
let Thing3 : #IFoo = Foo() // warning - and overrides the type signature!!
let Thing3 : IFoo = upcast(Foo()) // works!
let Thing4 : IFoo = downcast(Foo()) // doesn't work!
let Thing5 : IFoo = Foo() :> IFoo // works!

@isaacabraham
Copy link
Contributor

isaacabraham commented May 3, 2016

I propose the error message to say: -

"The conversion from <type A> to <type B> is a compile-time safe upcast, not a downcast. Considering using the :> (upcast) operator instead of the :?> (downcast) operator."

forki added a commit to forki/visualfsharp that referenced this issue May 4, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
@forki
Copy link
Contributor

forki commented May 9, 2016

PR in #1149

forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 9, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 10, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 11, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 12, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 12, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 12, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 12, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 12, 2016
forki added a commit to forki/visualfsharp that referenced this issue May 13, 2016
KevinRansom added a commit that referenced this issue May 18, 2016
Propose to use :> instead of :?> - fixes #1127 

Thank you for taking care of this.

Kevin
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