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

Technology CommunityCategory: KotlinWhat is the purpose of Unit-returning in functions? Why is VALUE there? What is this VALUE?
VietMX Staff asked 3 years ago
Problem

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

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 
}

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
  4. UNIT actually contains valuable information, it basically just means “DONE”. It just returns the information to the caller, that the method has been finished.