unit-type

How to return Ok unit type of std::result<(), E>?

泪湿孤枕 提交于 2021-01-21 06:43:59
问题 If I define a function: fn f() -> Result<(), E> { // How to return Ok()? } How can I return the Ok in std::result with the unit type () ? 回答1: The only value of type () is () , so just put that inside the Ok constructor: fn f() -> Result<(), E> { Ok(()) } 回答2: Use Ok(()) as in fn f() -> Result<(), E> { Ok(()) } 来源: https://stackoverflow.com/questions/27945858/how-to-return-ok-unit-type-of-stdresult-e

What are the differences between the multiple ways to create zero-sized structs?

寵の児 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

杀马特。学长 韩版系。学妹 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

自作多情 提交于 2020-12-02 05:54:04
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

F# - The type int is not compatible with type unit

落花浮王杯 提交于 2020-01-03 09:00:10
问题 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 is of type System.Windows.Controls.Image, and grid is System.Windows.Controls.Grid. Again, error is: The type int is not compatible with type unit 回答1: F# does not allow for you to silently ignore return values.

F# how to write an empty statement

∥☆過路亽.° 提交于 2020-01-02 00:54:31
问题 How can I write a no-op statement in F#? Specifically, how can I improve the second clause of the following match statement: match list with | [] -> printfn "Empty!" | _ -> ignore 0 回答1: Use unit for empty side effect: match list with | [] -> printfn "Empty!" | _ -> () 回答2: The answer from Stringer is, of course, correct. I thought it may be useful to clarify how this works, because "()" insn't really an empty statement or empty side effect... In F#, every valid piece of code is an expression

How to implement an interface member that returns void in F#

我怕爱的太早我们不能终老 提交于 2020-01-01 23:59:08
问题 Imagine the following interface in C#: interface IFoo { void Bar(); } How can I implement this in F#? All the examples I've found during 30 minutes of searching online show only examples that have return types which I suppose is more common in a functional style, but something I can't avoid in this instance. Here's what I have so far: type Bar() = interface IFoo with member this.Bar() = void Fails with _FS0010: Unexpected keyword 'void' in expression_. 回答1: The equivalent is unit which is

This expression should have type 'unit', but has type 'ConsoleKeyInfo'

蓝咒 提交于 2019-12-28 12:57:41
问题 I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. What can I do to fix this? 回答1: Solution: Console.ReadKey() |> ignore Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing

This expression should have type 'unit', but has type 'ConsoleKeyInfo'

試著忘記壹切 提交于 2019-12-28 12:56:51
问题 I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. What can I do to fix this? 回答1: Solution: Console.ReadKey() |> ignore Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing

F# - Function with no arguments?

笑着哭i 提交于 2019-12-18 05:27:25
问题 When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value. However, reallity gets in the way, and with different inputs, I might not need a certain function, and if that function is computationally expensive, I'd like to not evaluate it if it's not needed. I found a workaround, using let func _ = ... and calling it with func 1 or whatever, but that feels very non-idiomatic and confusing to the