Scala Implicits and Type Classes 1 — Questions and Answers
Question 1: Which keyword is used to define an implicit value in Scala 2?
- implicit (Correct answer)
- given
- using
- infer
Correct answer: implicit
In Scala 2, the `implicit` keyword marks a value, method, or class so the compiler can supply it automatically when required.
Question 2: In Scala 3, which keyword replaces `implicit val` for defining implicit instances?
- implicit
- given (Correct answer)
- using
- auto
Correct answer: given
Scala 3 introduces `given` as a cleaner replacement for `implicit val`, making the intent of providing a type class instance explicit.
Question 3: What best describes a type class in Scala?
- A class that extends multiple traits simultaneously
- A pattern using traits and implicits to achieve ad-hoc polymorphism (Correct answer)
- A class defined with the `type` keyword
- A sealed abstract class with bounded type parameters
Correct answer: A pattern using traits and implicits to achieve ad-hoc polymorphism
A type class is a trait parameterized by a type, with implicit instances providing implementations, enabling polymorphism without inheritance.
Question 4: Which of the following correctly declares an implicit parameter in Scala 2?
- def foo(implicit x: Int): Unit (Correct answer)
- def foo(given x: Int): Unit
- def foo[implicit x: Int]: Unit
- def foo(@implicit x: Int): Unit
Correct answer: def foo(implicit x: Int): Unit
In Scala 2, `implicit` is placed at the start of a parameter list to mark all parameters in that list as implicit.
Question 5: What happens when the Scala compiler cannot find a required implicit value in scope?
- It uses null as the default value
- It throws a runtime NullPointerException
- It produces a compile-time error (Correct answer)
- It searches the runtime classpath for the implicit
Correct answer: It produces a compile-time error
Missing implicits are caught at compile time; the compiler reports an error stating it could not find an implicit value for the required type.
Question 6: What is an implicit conversion in Scala?
- A conversion that occurs automatically at JVM bytecode level
- A method marked `implicit` that the compiler applies automatically to convert one type to another (Correct answer)
- A conversion requiring an explicit cast with `asInstanceOf`
- A built-in conversion between primitive types and their boxed counterparts
Correct answer: A method marked `implicit` that the compiler applies automatically to convert one type to another
An implicit conversion is an `implicit def` (or `implicit class`) that the compiler inserts automatically when a type mismatch would otherwise cause a compile error.
Question 7: What is the purpose of the `implicitly[T]` method in Scala 2?
- To mark a method as implicit
- To summon an implicit value of type T from the current scope (Correct answer)
- To convert a value to type T implicitly
- To define an implicit class wrapping type T
Correct answer: To summon an implicit value of type T from the current scope
`implicitly[T]` is defined as `def implicitly[T](implicit e: T): T = e` and is used to retrieve an implicit value without naming the parameter.
Which keyword is used to define an implicit value in Scala 2?