Please help to correct below code getting error

Please help to correct below code

object ImplicitParams1 extends App {
val input = args(0).toInt
val x: Int =x
def find(implicit y : Int) = y
input = x * y
println(multiply(input))
}

we need to perform

Write a function multiply , which takes two integer variables x and y and returns an output in which y is an implicit parameter , and also declares the implicit parameter in the main function as 5 .

$ scalac ImplicitParams1.scala
ImplicitParams1.scala:5: error: reassignment to val
input = x * y
^
ImplicitParams1.scala:6: error: not found: value multiply
println(multiply(input))
^
two errors found

Hi,
There are several problems. First of all, why do you have input = x * y, i.e., why are you trying to reuse input (it is ilegal since it is a val, but not sensible in any case). Perhaps you want val result = ... instead. Further, if y has to be found implicitly, I assume what you want is def multiply(x: Int) = x * find.
Indeed this is a one-liner: def multiply(x: Int)(implicit y: Int) = x * y, though perhaps it would be good if you read some documentation for scala to figure out why this works.

regards,
Siddhartha

Tired below code still issue

object ImplicitParams1 extends App {
val input = args(0).toInt
def multiply(x: Int)(implicit y: Int) = x * y
println(multiply(input))
}

Do you mean an issue when you run this? That will presumably be because there is no implicit int in scope. As frequently said in this forum, still issue is highly unhelpful, instead please say what environment you ran stuff in and what was the error. Also please enclose code in triple quotes.

regards,
Siddhartha

Running below code which is throwing error -


object ImplicitParams1 extends App {
val input = args(0).toInt
def multiply(x: Int)(implicit y: Int) = x * y
println(multiply(input))
}

error is

$ scalac ImplicitParams1.scala
ImplicitParams1.scala:4: error: could not find implicit value for parameter y: Int
println(multiply(input))
^
one error found

You need an implicit in scope. For instance, you can have a statement implicit val myFavouriteInteget: Int = 3141 before def multiply ..., and the program will run. By the way, you also have to pass an integer as an argument.

Written code as


object ImplicitParams1 extends App {
val input = args(0).toInt
def multiply(x: Int = 1 )(implicit y: Int = input) = x * y
println(multiply(input))
}

it is still not working, anything wrong in it

Correct code will be


object ImplicitParams1 extends App {

val input = args(0).toInt
def multiply(x: Int = input )(implicit y: Int = 5) = x * y
println(multiply(input))
}

Assuming that your goal is to use implicits, the above code is not correct. It will run fine even if you remove the word implicit, which plays no role here. Instead the default value for y is used.

The correct version of your code is

object ImplicitParams1 extends App {
val input = args(0).toInt
implicit val z = 5
def multiply(x: Int)(implicit y: Int) = x * y
println(multiply(input))
}

By the way, please use fenced code blocks when posting code (not boldface). Also, you can look at documentation for implicits. The examples there are more complicated than the above since having an implicit Int floating around is usually bad practice in scala (though may be fine for exercises of the sort you are trying here).

regards,
Siddhartha

1 Like