How to remove a parenthesis from an output in Scala

Multi tool use
How to remove a parenthesis from an output in Scala
I have the following output:
The food order cost is:
$84.9
()
from this function:
def foodBill(foodCost: List[Double]) = {
if (foodCost.length <= 1) {
println("The food order cost is: ")
foodTotal.foreach(x=>println("$"+ x))
} else {
println("The food order for each table is: ")
foodCost.foreach(x => println("$"+ x))
}
}
Thus, my question is how to remove the () from the output and have the following:
The food order cost is:
$84.9
Thanks
And what are you trying to achieve?
– user152468
Jun 30 at 11:01
@user152468, Im calling it as follows: println(foodBill(foodTotal)) and I'm trying to print out the food bill.
– mike
Jun 30 at 11:04
@user152468, is there a way without using an object
– mike
Jun 30 at 11:04
1 Answer
1
Instead of
println(foodBill(List(84.9)))
you should invoke it just as
foodBill(List(84.9))
This is not some kind of stringly-typed bash script, printing strings to the standard output is not the same as returning a value.
why is the unit type is returning from my function?????. Thanks for the answer.
– mike
Jun 30 at 11:07
@mike It returns
Unit
because every branch of the if
returns a Unit
. Each branch returns Unit
because foreach
returns Unit
. That's why you should ascribe the return type explicitly. If you had declared it as def foodBill(foodCost: List[Double]): Unit
, there wouldn't be any questions.– Andrey Tyukin
Jun 30 at 11:08
Unit
if
Unit
Unit
foreach
Unit
def foodBill(foodCost: List[Double]): Unit
I know but why is type being outputted
– mike
Jun 30 at 11:10
What do you mean by "type being outputted"?
()
is not a type. It's the only value of type Unit
, returned by the foreach
.– Andrey Tyukin
Jun 30 at 11:11
()
Unit
foreach
yes you have answered my question. I thought () this was a type but it was a value. I understand now. Thanks again
– mike
Jun 30 at 11:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How do you call your function?
– user152468
Jun 30 at 10:58