Printing main method in Scala3(Dotty) gave value as Main$$$Lambda ... Why? - scala

I wrote a code below in Scala .
object Main {
def main(args: Array[String]): Unit = {
println(main)
}
}
It gave output as :
Main$$$Lambda$3/1543727556#3930015a
Why is this value returned ?
Also, When I wrote a simple method like one below :
def wow(): Unit = {
println(wow)
}
But it gives compilation error.

Related

Calling main methods in other objects in Scala

Is it possible to call a main method in one object from a main method in another? I have the following classes and was wondering how to call two separate main methods within one program run:
object MongoUpload {
def main(args: Array[String]): Unit = {
.. upload to Mongo ..
// Want to upload to Oracle here
}
}
object OracleUpload {
def main(args: Array[String]): Unit = {
.. upload to Oracle
}
}
Does anything make main unique among methods? Can I just call one from another?
You sure can. Just like any other method, main can be called between objects.
object foo {
def main(args: Array[String]): Unit = {
println("qux")
}
}
object bar {
def main(args: Array[String]): Unit = {
println("baz")
foo.main(null)
}
}
Running main in bar gives the following output:
baz
qux
The same can also be replicated to main methods with arguments, as in the following example:
object foo {
def main(args: Array[String]): Unit = {
println(args(0) + " " + args(1))
}
}
object bar {
def main(args: Array[String]): Unit = {
... some processing ...
foo.main(Array["Hello", "World"])
}
}
Running main in bar gives the following output:
Hello World
Whether or not it leads to clear and readable code is another question :)

Scala - Global variable get the value in a function

I am a beginner of Scala.
I immediately get a problem.
Let's say:
I have a Vector variable and 2 functions. The first one function is calling 2nd function. And there is a variable in 2nd function is what I need to get and then append it to Vector. (Without returning the variable in 2nd function.)
The structure looks like this:
def main(args: Array[String]): Unit = {
var vectorA = Vector().empty
}
def funcA(): sometype = {
...
...
...
funcB()
}
def funcB(): sometype = {
var error = 87
}
How can I add error variable in global Vector?
I tried to write vectorA :+ error but in vain.
You could do the following:
def main(args: Array[String]): Unit = {
val vectorA = funcA(Vector.empty)
}
def funcA(vec: Vector): sometype = {
...
...
...
funcB()
}
def funcB(vec: Vector): sometype = {
// Here you could append, which returns a new copy of the Vector
val error = 87
vec :+ error
}
Keep in mind that, it is advisable to use immutable variables. Though not always this might be true, but for most of the applications that just involve doing some CRUD type logic, it is better to use immutable variables.

Scala - Timer Error

I'm new on Scala and I'm trying to pass a function/method as parameter to another by using unit, but it gives me the following error:
Timer.<error: >
My code is the following:
object Timer {
def oncePerSecond(callback: () => unit) {
while (true) {
callback(); Thread sleep 1000
}
}
def timeFlies() {
println("The time passes...")
}
def main(args: Array[String]) {
oncePerSecond(timeFlies)
}
}
But I'm certainly my code is correct and I don't understand why I'm getting this. Can someone help me to find this bug?
The error seems to be in the word "unit" on line 2.
Unit with a capital U fixes the error. Classes in Scala begin with a capital letter.

Simple scala code: Returning first element from string array

I don't know how to fix this code. It "explodes" somewhere in returnFirstString but I don't know why. Also, I don't know how to properly display result using println. Is this approach ok.
So here's the code:
def returnFirstString(a: Array[String]): Option[String]=
{
if(a.isEmpty) { None }
Some(a(0))
}
val emptyArrayOfStrings = Array.empty[String]
println(returnFirstString(emptyArrayOfStrings))
You're not properly returning the None:
def returnFirstString(a: Array[String]): Option[String] = {
if (a.isEmpty) {
None
}
else {
Some(a(0))
}
}
Also, there's already a method for this on most scala collections:
emptyArrayOfStrings.headOption
The most concise way:
def returnFirstString(a: Array[String]): Option[String]= a.headOption

Getting "error: type mismatch; found : Unit required: () => Unit" on callback

I am just starting out going through a tutorial on scala and have hit a block. I have merged together a couple of examples and am getting an error, but don't know why.
import java.text.DateFormat._
import java.util.{Date, Locale}
object FrenchDate {
def main(args: Array[String]) {
timer(println(frenchDate))
}
def frenchDate():String = {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
df format now
}
def timer(callback: () => Unit) {
while(true) {callback(); Thread sleep 1000}
}
}
Brings the error
error: type mismatch;
found : Unit
required: () => Unit
println(frenchDate)
while the below works
import java.text.DateFormat._
import java.util.{Date, Locale}
object FrenchDate {
def main(args: Array[String]) {
timer(frenchDate)
}
def frenchDate() {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
def timer(callback: () => Unit) {
while(true) {callback(); Thread sleep 1000}
}
}
The only difference is that the date is printed out in frenchDate() in the second once whereas it is returned and printed in the callback on the first.
The difference is that this line:
timer(println(frenchDate))
is trying to call println(frenchDate) and use the return value (which is Unit) as the callback to pass to timer. You probably want:
timer(() => println(frenchDate))
or possibly
timer(() => { println(frenchDate) })
(I'm not a Scala dev, so I'm not sure of the right syntax, but I'm pretty confident about what's wrong in your current code :)
EDIT: According to comments, this should work too and may be more idiomatic:
timer { () => println(frenchDate) }