F#-类型int与类型单位不兼容 [英] F# - The type int is not compatible with type unit

查看:143
本文介绍了F#-类型int与类型单位不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能语言非常新,但是我正在使用许多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#不允许您默默地忽略返回值.类型unitvoid的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

我希望该语句不返回任何值,而是返回一个int值

I expected the statement to have no return but instead it returns an int value

或相反.我倾向于错误地阅读此错误消息.

Or the opposite. I tend to incorrectly read this error message.

可能发生的是以下其中一种

What's likely happening is one of the following

  1. 有问题的方法期望返回值int,但是方法Add返回空值,因此F#只是要求返回值
  2. 有问题的方法键入为unit,但是Add返回一个int,F#需要您忽略该值.
  3. GetCaptureToWpfImage返回需要显式处理的值.
  1. The method in question is expecting an int return value but the method Add returns void hence F# is just asking for a return value
  2. The method in question is typed as unit but Add is returning an int and F# needs you to ignore the value.
  3. The GetCapture or ToWpfImage 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与类型单位不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆