unit-type

Why do I need to use the unit type in F# if it supports the void type?

◇◆丶佛笑我妖孽 提交于 2019-11-29 09:27:55
I read this MSDN article: Unit Type (F#) ...The unit type is a type that indicates the absence of a specific value; the unit type has only a single value, which acts as a placeholder when no other value exists or is needed ... The unit type resembles the void type in languages such as C# and C++... So... Alright, I understand, that the unit type is such a type, which has only a single value () . But I have some questions: Why is it needed? When is it needed? I don't understand why not to use the void type in F#, like C# and C++ use. If I look at the following table: Primitive Types (F#) Type

What is the purpose of the unit type in Rust?

女生的网名这么多〃 提交于 2019-11-29 05:25:56
Rust has the unit type , () , a type with a single zero-size value. The value of this unit type is also specified using () . What is the purpose of the unit type and its value? Is it a mechanism to avoid using null (or nil ) like other languages have? A.B. () is a value of the type () and its purpose is to be useless. Everything in Rust is an expression, and expressions that return "nothing" actually return () . The compiler will give an error if you have a function without a return type but return something other than () anyway. For example fn f() { 1i32 // error: mismatched types: expected `

What is () in Haskell, exactly?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 16:03:24
I'm reading Learn You a Haskell , and in the monad chapters, it seems to me that () is being treated as a sort of "null" for every type. When I check the type of () in GHCi, I get >> :t () () :: () which is an extremely confusing statement. It seems that () is a type all to itself. I'm confused as to how it fits into the language, and how it seems to be able to stand for any type. pigworker tl;dr () does not add a "null" value to every type, hell no; () is a "dull" value in a type of its own: () . Let me step back from the question a moment and address a common source of confusion. A key thing

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

你说的曾经没有我的故事 提交于 2019-11-28 08:02:14
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? 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. It could be defined like this: let ignore _ = () 来源: https://stackoverflow.com/questions/324947/this

Scala: Why can I convert Int to Unit?

做~自己de王妃 提交于 2019-11-28 00:42:01
I've recently started playing with Scala (2.8) and noticed the I can write the following code (in the Scala Interpreter): scala> var x : Unit = 10 x : Unit = () It's not obvious what's going on there. I really didn't expect to see any implicit conversion to Unit . See section "6.26.1 Value Conversions" in the Scala Language Specification version 2.8: ... Value Discarding. If e has some value type and the expected type is Unit, e is converted to the expected type by embedding it in the term { e; () } . ... Anything can be converted to Unit. This is mostly necessary to support side-effecting

What is the purpose of the unit type in Rust?

半世苍凉 提交于 2019-11-27 22:56:16
问题 Rust has the unit type , () , a type with a single zero-size value. The value of this unit type is also specified using () . What is the purpose of the unit type and its value? Is it a mechanism to avoid using null (or nil ) like other languages have? 回答1: () is a value of the type () and its purpose is to be useless. Everything in Rust is an expression, and expressions that return "nothing" actually return () . The compiler will give an error if you have a function without a return type but

What does this '()' notation mean?

点点圈 提交于 2019-11-27 09:41:31
I just started to learn F#. The book uses the following notation: let name() = 3 name() what that differs from this: let name = 3 name ? Before answering what () is lets get some basics defined and some examples done. In F# a let statement has a name, zero or more arguments, and an expression. To keep this simple we will go with: If there are no arguments then the let statement is a value . If there are arguments then the let statement is a function . For a value, the result of the expression is evaluated only once and bound to the identifier; it is immutable. For a function, the expression is

Scala: Why can I convert Int to Unit?

。_饼干妹妹 提交于 2019-11-26 21:46:13
问题 I've recently started playing with Scala (2.8) and noticed the I can write the following code (in the Scala Interpreter): scala> var x : Unit = 10 x : Unit = () It's not obvious what's going on there. I really didn't expect to see any implicit conversion to Unit . 回答1: See section "6.26.1 Value Conversions" in the Scala Language Specification version 2.8: ... Value Discarding. If e has some value type and the expected type is Unit, e is converted to the expected type by embedding it in the

F# interface inheritance failure due to unit

情到浓时终转凉″ 提交于 2019-11-26 17:48:53
问题 Does anyone know why this fails to compile? type MyInterface<'input, 'output> = abstract member MyFun: 'input -> 'output type MyClass() = interface MyInterface<string, unit> with member this.MyFun(input: string) = () //fails with error FS0017: The member 'MyFun : string -> unit' does not have the correct type to override the corresponding abstract method. type MyUnit = MyUnit type MyClass2() = //success interface MyInterface<string, MyUnit> with member this.MyFun(input: string) = MyUnit 回答1:

What does this &#39;()&#39; notation mean?

拈花ヽ惹草 提交于 2019-11-26 14:48:48
问题 I just started to learn F#. The book uses the following notation: let name() = 3 name() what that differs from this: let name = 3 name ? 回答1: Before answering what () is lets get some basics defined and some examples done. In F# a let statement has a name, zero or more arguments, and an expression. To keep this simple we will go with: If there are no arguments then the let statement is a value. If there are arguments then the let statement is a function. For a value, the result of the