Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Unit-returning in functions

Tags:

kotlin

From the Kotlin documentation:

If a function does not return any useful value, its return type is Unit. Unit is a type with only one value — Unit.VALUE. This value does not have to be returned explicitly:

fun printHello(name : String?) : Unit {     if (name != null)       print("Hello, $name!")     else       print("Hi there!")     // We don't need to write 'return Unit.VALUE' or 'return', although we could  } 

What is the purpose of Unit-returning in functions? Why is VALUE there? What is this VALUE?

like image 973
MajesticRa Avatar asked Mar 26 '14 08:03

MajesticRa


People also ask

What is the use of unit in Kotlin?

Unit: Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

How do you use nothing in Kotlin?

Nothing has no instances. You can use Nothing to represent "a value that never exists": for example, if a function has the return type of Nothing, it means that it never returns (always throws an exception).

What is it keyword in Kotlin?

Using “it” keyword in place of a single parameter in Lambdas expressions. Learn about Lambdas Expressions and High-Level Functions. Kotlin as an Android Programming Language. Kotlin supports Functional programming where you can pass a function as a parameter and even return a function. Smartherd.


2 Answers

The purpose is the same as C's or Java's void. Only Unit is a proper type, so it can be passed as a generic argument etc.

  1. Why we don't call it "Void": because the word "void" means "nothing", and there's another type, Nothing, that means just "no value at all", i.e. the computation did not complete normally (looped forever or threw an exception). We could not afford the clash of meanings.

  2. Why Unit has a value (i.e. is not the same as Nothing): because generic code can work smoothly then. If you pass Unit for a generic parameter T, the code written for any T will expect an object, and there must be an object, the sole value of Unit.

  3. How to access that value of Unit: since it's a singleton object, just say Unit

like image 98
Andrey Breslav Avatar answered Sep 27 '22 23:09

Andrey Breslav


The main reason why Unit exists is because of Generic reasons. Let's use the example from the Kotlin docs.

class Box<T>(t: T) {     var value = t } 

We can have

var box = Box(Unit) 

This is why Unit returns a value so the Kotlin can infer it from the type passed into class initialization. Of course, you could also explicitly write it like this,

var box = Box<Unit>(Unit) 

but all the same, it must have a return value. Now, the void keyword in Java in Kotlin is Nothing. Nothing is the last but one type in the type hierarchy in Kotlin with the last one being Nothing? (Nullable Nothing). This does not return any value at all. Because it doesn't return any value at all, we can't pass it as a type in the above code.

var box = Box(Nothing) //This will return an Error 
like image 25
Alf Moh Avatar answered Sep 28 '22 00:09

Alf Moh