Updated April 4, 2023
Introduction to Scala Unit
Unit act in the same way as a void in java. void used in those functions that do not return anything or are not supposed to return any value in return. The unit type works in the same way as the void that means it also does not return anything. The unit type is a child class of any type in the trait. This is used with the method signature in the scala as a return value. We should use this when we do not want our function to return any value. In this topic, we are going to learn about Scala Unit.
Syntax
To define any function with the unit type, we just need to use the ‘Unit’ keyword with the method signature. We will also see one practice example to understand the syntax.
def method_name(variavle_name: Data_type): Retunr_type = {
// logic goes here
}
Example:
def getM(msg: String): Unit = {
println("Msg goes here ")
}
In the above lines of code, we are using the Unit keyword to define our function, which is not going to return anything; it will just execute the logic inside the body of the function.
How does unit type work in Scala?
The unit is a return type in scala, and it acts as a void in another programming language like java. Now we will see one example to understand it better. We should go for unit type when we are not returning anything from our function; we are just executing our logic and updating some values that can be anything also include some database calls or running a method in a separate thread.
In scala, it also supports some other keywords as well to work with the empty value see below;
- Nothing: This is also a trait, which is responsible for throwing an exception. This trait also has no instance.
- null: Double, Int, Long cannot be nullable, but String and Object can be nullable.
- Null: By using this, we can assign the reference type as null, but here also value cannot be null.
- Unit: In this, as we discussed, it does not return any value.
Now we will see one full functioning program to understand its working and how we can use this in our program;
Example:
object Main extends App{
// Your code here!
def getMessage(msg: String): Unit = {
println("this msg from unit demo "+ msg)
}
println("result is ::")
println(getMessage("hello !!"))
}
In the above lines of code, we are creating one main object here named as main and extending the App class from scala. After that, we are creating the getMessage method by using the def keyword. This method takes one parameter, which is of string type. But the return type for this function is unit that means this function will not return any value when it called; it is a simple function without a return statement. At the last of the program, we are just calling this function from the method println already available in scala. So it will print out the line of code written inside the body of the function and execute the whole logic for us. But we can call another function from this whose return type is not Unit; this is not restricted in any programming language; it depends upon our requirement.
Point to remember while working with Unit type in scala;
1) If you want to define a function that does not return any value, then we should go for Unit in scala.
2) To make a function that does not return value, we have to use the Unit keyword there.
3) If you want some value in return from your function, does not go for Unit type because it is the same as a void in other programming languages like java and so on.
4) It is used to represent the Null value in scala, or we can say no value at all.
Examples of Scala Unit
Given below are the examples of Scala Unit:
Example #1
In this example, we are using a unit function taking one parameter, which is type String.
Code:
object Main extends App{
// Your code here!
//defining function here
def getMessage(msg: String): Unit = {
println("this msg from unit demo "+ msg)
}
// printing output here
println("result is ::")
println(getMessage("hello !!"))
}
Output:
Example #2
In this example, we are using a unit function and trying to find out the sum of the variable.
Code:
object Main extends App{
// Your code here!
//defining function here ..
def getMessage(x: Int, y: Int): Unit = {
println("Inside function ..")
var z = x + y
println("result is ::")
println(z)
}
// printing output here ..
println("result is ::")
println(getMessage(20 , 30 ))
}
Output:
Example #3
In this example, we have defined as many functions with return type Units. Also, we are calling another function from one another.
Code:
object Main extends App{
// Your code here!
//defining function here ..
def getMessage(msg: String): Unit = {
println("this msg from unit demo "+ msg)
}
def callAnother(msg: String): Unit = {
println("calling another function from here ..")
println(changefun("hello by another function ..."))
}
def getMultiply(x: Int , y: Int, z: Int): Unit = {
println("calculating multiplication of values ;::")
var result = x * y * z
println("result is ::")
println(result)
}
def changefun(msg: String): Unit = {
println("this is another function from one function :: ")
}
getMessage("hello msg from one method ::")
callAnother("calling another function from here ... ")
getMultiply(10, 10 , 10)
}
Output:
Example #4
We are calling two different methods here.
Code:
object Main extends App{
// Your code here!
//defining function here also using unit type here
def m1(msg1: String): Unit = {
println("this is method one "+ msg1)
}
def m2(msg2: String): Unit = {
println("this is method two "+ msg2)
}
// calling method here to print the result
m1("Method 1")
m2("Method 2")
}
Output:
Conclusion
Scala unit type is used when there is nothing to return. It can be stated as null, no value, or nothing in return. Scala provides other types also to deal with it like null, nil, nothing, and none. So these types used to show any empty return type value in scala. This functionality provided by the scala is very much similar to the void keyword available in other programming languages.
Recommended Articles
We hope that this EDUCBA information on “Scala unit” was beneficial to you. You can view EDUCBA’s recommended articles for more information.