Base question:
Why can I write in Scala just:
println(10)
Why don’t I need to write:
Console println(10)
Followup question:
How can I introduce a new method “foo” which is everywhere visible and usable like “println”?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You don’t need to write the
Consolein front of the statement because the ScalaPredefobject, which is automatically imported for any Scala source file, contains definitions like these:You cannot easily create a “global” method that’s automatically visible everywhere yourself. What you can do is put such methods in a package object, for example:
But to be able to use it, you’d need to import the package:
(Note: Instead of a package object you could also put it in a regular object, class or trait, as long as you import the content of the object, class or trait – but package objects are more or less meant for this purpose).