使用 F# 的可选参数和选项类型
考虑以下代码:
type Test () =
member o.fn1 (?bo) = 1
member o.fn2 (?bo) = o.fn1 bo
member o.fn3 (?bo) = 1 + bo.Value
member o.fn4 (?bo) = o.fn3 bo
虽然 fn1
和 fn2
工作正常,但 fn4
会产生以下错误:
init.fsx(6,30): 错误 FS0001: 该表达式的类型应为 int,但此处的类型为“a option”
MSDN 指出:
可选参数被解释为 F# 选项类型,因此您可以通过使用具有 Some 和 None 的匹配表达式,以查询选项类型的常规方式查询它们。
对我来说,可选参数不会被解释为 F# 选项类型,否则代码会编译。此外,我不明白为什么,当我将鼠标悬停在 fn3
中的 ?bo
上时,工具提示显示 val bo: int option
但从外部只期望 <代码>int。我期望一种不接受任何内容、int、Some int 和 None 的行为。最后一点,我不明白为什么 fn2
有效,但 fn4
无效。
感谢您的澄清
Consider the following code:
type Test () =
member o.fn1 (?bo) = 1
member o.fn2 (?bo) = o.fn1 bo
member o.fn3 (?bo) = 1 + bo.Value
member o.fn4 (?bo) = o.fn3 bo
While fn1
and fn2
work just fine, fn4
produces the following error:
init.fsx(6,30): error FS0001: This expression was expected to have type int but here has type 'a option
MSDN states:
Optional parameters are interpreted as the F# option type, so you can query them in the regular way that option types are queried, by using a match expression with Some and None.
To me, optional parameters are not interpreted as the F# option type otherwise the code would compile. Moreover I do not understand why, when I hover over ?bo
in fn3
the tooltip says val bo: int option
but from outside expects only int
. I would expect a behavior of accepting nothing, int, Some int and None. And as the last note, I do not understand why fn2
works but fn4
does not.
Thanks for clarification
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须重新考虑正确答案。基于这个问题(和答案):
传播可选参数
似乎正确的答案如下:
这是一个简洁的功能,答案请访问 desco!
I have to reconsider the correct answer. Based on this question (and answer):
Propagating optional arguments
it seams that the correct answer is the following:
It is a neat feature and credits for the answer go to desco!
fn2
之所以有效,是因为fn1
不使用其参数,因此它是通用的'b 选项
。fn4
抱怨传递给fn3
的参数应该是 int,而不是int option
因为当您指定参数时,您的当然需要传入一个特定的。但您可以选择省略该参数。fn3
的定义/类型签名不知道您是否指定了bo
,因此它是一个int 选项
。请注意,您可能有以下用法:您没有为
fn3
指定参数,但指定它时,它是一个具体的int
,而不是int 选项
。考虑一个具有三个参数的绘图函数:
因为参数是可选的,所以您可以有以下用法:
但不是:
fn2
works becausefn1
does not use its parameter, which is thus generic'b option
.fn4
complains that the parameter passed tofn3
should be an int, but notint option
because when you specify the parameter, you of course need to pass in a specific one. But you have the option to omit the parameter. The definition/type signature offn3
does not know whether you have specifybo
or not, so it is aint option
. Notice that you may have the following usage:where you don't specify the parameter for
fn3
, but when you specify it, it is a concreteint
, notint option
.Think about a plotting function with three parameters:
because the parameters are optional, you can have the following usage:
But not:
F# 不需要匹配来传递可选参数。
使用绘图示例:
上面的示例允许您使用自己的可选参数调用绘图。
/JEE,夏普#Soft
F# does not require matching to pass optional arguments.
Using the plot-example:
The above lets you call plot with your own optional arguments.
/JEE, Sharp#Soft