Unclear function return type issue

夙愿已清 提交于 2019-12-10 18:12:24

问题


I have the following rather simple F# function:

let FormatValue (formatProvider : IFormatProvider) valueSuffix value =
    match value > Convert.ToDecimal(valueSuffix.MinimumValueRequired) with
    | true -> let normalizedValue = Convert.ToDecimal(value) / Convert.ToDecimal((Math.Pow(10., Convert.ToDouble(valueSuffix.PowerOfTen)))) in
                  string.Format("{0}{1}", normalizedValue.ToString(valueSuffix.Format, formatProvider), valueSuffix.Text)
    | false -> ""

The return type is correctly inferred as string, however I get an error marker at string.Format in the true branch, saying the type <'a> -> string is not compatible with type ValueSuffix. I find this especially surprising as all other types are inferred correctly, and in particular there is no other occurrence of <'a> in the function.

What am I doing and/or understanding wrong?


回答1:


The issue was that string.Format is not valid in F# code.

You need to either use

System.String.Format

or

open System
.....
String.Format

(The difference is upper versus lower case s in string




回答2:


John Palmer's answer is correct, but I've often wondered why string.Format is not valid in F# code, and until this question I had not bothered to investigate it.

Looking at the relevant source, we see that string is just a type alias for System.String. So it seems like we should be able to use it just like System.String. For example, suppose we define the following type alias:

type foo = System.String

This will allow us to do things like foo.Format without issue.

The problem is that not only is string defined as a type alias, it is also defined as conversion function. This effectively shadows the type alias except in contexts where only a type name could be expected (e.g. type annotations and casts).

We can demonstrate this by defining our own conversion function to shadow our foo type alias:

let foo value = 
    match box value with
    | null -> ""
    | _ -> value.ToString()

Now the aformentioned foo.Format call will not compile.

The same goes with all the other basic types (int, float, etc.).



来源:https://stackoverflow.com/questions/22417983/unclear-function-return-type-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!