问题描述
功能语言非常新,但是我正在使用许多F#维护他人的代码.谁能对此提供一些见识?
Quite new to functional languages, but I'm maintaining someone else's code with a lot of F#. Can anyone offer some insight into this?
let mtvCap = Rendering.MTViewerCapture(mtViewer)
mtvCap.GetCapture()
mtvCap.ToWpfImage()
grid.Children.Add(mtvCap.ImageElement)
MTViewer.ImageViewer的类型为System.Windows.Controls.Image,网格为System.Windows.Controls.Grid.
MTViewer.ImageViewer is of type System.Windows.Controls.Image, and grid is System.Windows.Controls.Grid.
同样,错误是:int类型与类型单位不兼容
Again, error is: The type int is not compatible with type unit
推荐答案
F#不允许您默默地忽略返回值.类型unit
是void
的F#版本.所以错误实际上是在说
F# does not allow for you to silently ignore return values. The type unit
is F#'s version of void
. So the error is saying essentially
或相反.我倾向于错误地阅读此错误消息.
Or the opposite. I tend to incorrectly read this error message.
可能发生的是以下其中一种
What's likely happening is one of the following
- 有问题的方法期望返回值
int
,但是方法Add
返回空值,因此F#只是要求返回值 - 有问题的方法键入为
unit
,但是Add
返回一个int
,F#需要您忽略该值. -
GetCapture
或ToWpfImage
返回需要显式处理的值.
- The method in question is expecting an
int
return value but the methodAdd
returns void hence F# is just asking for a return value - The method in question is typed as
unit
butAdd
is returning anint
and F# needs you to ignore the value. - The
GetCapture
orToWpfImage
return values that need to be explicitly handled.
对于最后2种情况,您可以通过将值传递给ignore
函数
For the last 2 cases you can fix this by passing the value to the ignore
function
mtvCap.GetCapture() |> ignore
mtvCap.ToWpfImage() |> ignore
grid.Children.Add(mtvCap.ImageElement) |> ignore
深入研究之后,我相信#2是问题所在,因为UIElementCollection.Add
返回一个int
值.尝试将最后一行修改如下:
After digging around a bit I believe #2 is the issue because UIElementCollection.Add
returns an int
value. Try modifying the final line to look like this
grid.Children.Add(mtvCap.ImageElement) |> ignore
这篇关于F#-类型int与类型单位不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!