I propose we create a compiler warning, off-by-default, that warns when the typechecker infers a type to be obj without annotation.
Ideally, if there is some reason to expect that something is of type obj, then we would not warn. For example, the following code should not generate a warning, because a is constrained to be obj by the type argument to unbox, and b is constrained to be obj because it is of the same type as a.
let a = 5 |> unbox<obj>
let b = a
However, we would warn in the following case, where a is inferred to be obj because that's the most general thing it could be:
let f<'b> () : 'b =
let a = failwith ""
a |> unbox
The existing way of approaching this problem in F# is to ignore it and assume that you intended obj whenever obj is inferred (unless there's some other reason to think that you didn't mean it, such as unnecessary specialisation of a generic type as indicated by FS0064).
Pros and Cons
The advantages of making this adjustment to F# are that it causes us to catch a class of bug we currently can't catch. I have never encountered an instance where the compiler has inferred obj and my code was correct; using obj at all is sufficiently weird that it seems reasonable to expect it to be signposted with a type annotation somewhere (much like mutable is a required signpost for mutability). However, since this is a feature of F#, it seems reasonable to disable the warning by default.
The disadvantages of making this adjustment to F# are a slight increase in complexity during typecheck, since we need to work out whether a type has been specialised.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: none
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
I propose we create a compiler warning, off-by-default, that warns when the typechecker infers a type to be
objwithout annotation.Ideally, if there is some reason to expect that something is of type
obj, then we would not warn. For example, the following code should not generate a warning, becauseais constrained to beobjby the type argument tounbox, andbis constrained to beobjbecause it is of the same type asa.However, we would warn in the following case, where
ais inferred to beobjbecause that's the most general thing it could be:The existing way of approaching this problem in F# is to ignore it and assume that you intended
objwheneverobjis inferred (unless there's some other reason to think that you didn't mean it, such as unnecessary specialisation of a generic type as indicated by FS0064).Pros and Cons
The advantages of making this adjustment to F# are that it causes us to catch a class of bug we currently can't catch. I have never encountered an instance where the compiler has inferred
objand my code was correct; usingobjat all is sufficiently weird that it seems reasonable to expect it to be signposted with a type annotation somewhere (much likemutableis a required signpost for mutability). However, since this is a feature of F#, it seems reasonable to disable the warning by default.The disadvantages of making this adjustment to F# are a slight increase in complexity during typecheck, since we need to work out whether a type has been specialised.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: none
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply: