F # - The int type is not compatible with the type block

Brand new for functional languages, but I support the code of another with lots of F #. Can anyone tell about this?

let mtvCap = Rendering.MTViewerCapture(mtViewer) mtvCap.GetCapture() mtvCap.ToWpfImage() grid.Children.Add(mtvCap.ImageElement) 

MTViewer.ImageViewer is of type System.Windows.Controls.Image, and the grid is of type System.Windows.Controls.Grid.

Again, the error is this: the int type is not compatible with the unit of type

+7
f # unit-type
source share
2 answers

F # does not allow you to silently ignore returned values. The unit type is the version of F # void . So the error says it all

I expected the operator to have no return, but instead it will return an int value

Or vice versa. I am not reading this error message correctly.

This is most likely one of the following

  • This method expects an int return value, but the Add method returns void, so F # just asks for the return value
  • This method is introduced as unit , but Add returns int , and F # needs to ignore the value.
  • The return values ​​of GetCapture or ToWpfImage that must be explicitly processed.

In the last two cases, you can fix this by passing the value of the ignore function

 mtvCap.GetCapture() |> ignore mtvCap.ToWpfImage() |> ignore grid.Children.Add(mtvCap.ImageElement) |> ignore 

After digging around the bits, I believe problem # 2 is the problem, because UIElementCollection.Add returns an int value. Try changing the final line to look like this:

 grid.Children.Add(mtvCap.ImageElement) |> ignore 
+14
source share

I know very little about F #, but as I recall, “unit” is their way of saying “void”, so I'm going to assume that you are trying to assign a “return value” to a function, t is one to variable. This will make the most likely candidate, this line:

let mtvCap = Rendering.MTViewerCapture(mtViewer)

+1
source share

All Articles