I am aware that the Predef class is already in scope. It does not need to be imported. That is my understanding of this.
The following is a screenshot of methods directly lifted directly from the Scala API for Predef.
I would like to come up with simple use cases to write a sample program for each of those utility methods. I use “print”, “println” etc, and just take these for granted.
I need some help understanding what ??? does, what identity[A] does? On the same lines, how would I use “implicitly”
Thanks for any direction, ideas, etc
??? does nothing except throw an exception. The reason it’s useful is because it can be used as a placeholder for values of any type, to allow typechecking to succeed.
identity returns the same value as output that you give it as input. It’s useful in certain situations that require a function of the type
T => T to indicate that a value should be modified somehow, but sometimes you don’t want to actually modify the value, so you pass in
identity to get back the unmodified value.
implicitly returns an implicit value of the requested type
T (passed in by type parameter). It’s just a small convenience method like the others that you would normally use to test out implicits. Personally I’ve used in only in the REPL.
This answer on stackoverflow explains some uses of
implicitly. To summarize:
- Accessing implicits defined via context bound (
def foo[A](implicit ma: M[A]) can be written as
def foo[A : M], giving no name to the implicit parameter).
- Passing some implicit parameters to a function explicitly, while still getting the rest from implicit scope.
- Using it on the REPL to inspect which implicit conversion a method came from (doesn’t seem to work in current scala versions)
Thank you very much for your reply. I will work on writing some code for it to test my understanding of it.
Thanks for your explanation.
May I ask how you have used it in the REPL?
I have only liberally used those Console methods but never bothered to check methods like ???
Okay, the Stackoverflow answer is very useful for implicityly. Thanks for the direction