Turning off Predef

137 views
Skip to first unread message

Travis Parks

unread,
Sep 19, 2016, 2:38:22 PM9/19/16
to scala-debate
I've always been bothered by Predef (http://www.scala-lang.org/api/current/index.html#scala.Predef$). For the most part, I consider many of the implicits just part of the language. But printf and println really bother me. Calls to those functions shouldn't appear in most non-console applications. Worse, developers new to Scala end up using implicit conversations without even realizing it; this leads to confusion when they go to look up definitions but can't find them. Methods like assert and require really seem out-of-place. It'd be nice if I could make it difficult for other developers (and myself) to use these undesirable "default" features.

That said, I am okay with most of the Predef functionality... wrapping primitives, strings, collections, etc. It makes me wish Predef was broken out by responsibility: rich primitives, string wrapping, collections, testings, etc. It would be nice to then turn on only the features you wanted through a compiler flag. Obviously for backward compatibility, by default, all Predefs would be included. Perhaps this is already possible and I just don't know about it?

Artúr Poór

unread,
Sep 19, 2016, 2:56:56 PM9/19/16
to scala-...@googlegroups.com
There is a -Yno-predef flag already in the compiler, so could import the
necessary things only. That should do the trick.

Cheers,
Artúr

2016-09-19 20:38 keltezéssel, Travis Parks írta:
> I've always been bothered by *Predef*
> (http://www.scala-lang.org/api/current/index.html#scala.Predef$). For the
> most part, I consider many of the implicits just part of the language. But
> *printf* and *println* really bother me. Calls to those functions shouldn't
> appear in most non-console applications. Worse, developers new to Scala end
> up using implicit conversations without even realizing it; this leads to
> confusion when they go to look up definitions but can't find them. Methods
> like *assert* and *require* really seem out-of-place. It'd be nice if I

Seth Tisue

unread,
Sep 19, 2016, 3:25:46 PM9/19/16
to scala-debate
There is a (WIP) PR in this area at https://github.com/scala/scala/pull/5350

Seth Tisue / Scala team / Lightbend, Inc.

Travis Parks

unread,
Sep 19, 2016, 3:27:58 PM9/19/16
to scala-debate
That's nuts. I've read both Odersky's and Wampler's books and have done repeated google searches, and I never heard of this option.
I'm going to start working on my import statement that excludes the stuff I don't want. I'll post it later for others benefit.

The best place for this little tidbit of information would be on that page I linked above. :-)

Travis Parks

unread,
Sep 19, 2016, 3:56:53 PM9/19/16
to scala-debate
My build.sbt file will end up looking like this on new projects:

lazy val commonSettings = Seq(
    scalaVersion
:= "2.11.8",
    scalacOptions
++= Seq(
       
"-unchecked",
       
"-deprecation",
       
"-Xfatal-warnings",
       
"–Xlint",
       
"-Yno-predef"
   
)
)

Unfortunately, the code to partially include/exclude some of the Predef stuff is ugly:

import scala.Predef.{
   
assert => _,
    assume
=> _,
   
print => _,
    printf
=> _,
    println
=> _,
   
require => _,
    _
}
import scala.Console

object Main extends App {
   
Console.out.println("Hello, World!")
}

If I could globally apply that nasty import to every .scala file, that'd be awesome to say the least.

Simon Schäfer

unread,
Sep 19, 2016, 4:02:44 PM9/19/16
to Travis Parks, scala-debate



---- On Mon, 19 Sep 2016 21:56:53 +0200 Travis Parks <jehuga...@gmail.com> wrote ----
> My build.sbt file will end up looking like this on new projects:
>
> lazy val commonSettings = Seq(
> scalaVersion := "2.11.8",
> scalacOptions ++= Seq(
> "-unchecked",
> "-deprecation",
> "-Xfatal-warnings",
> "–Xlint",
> "-Yno-predef"
> )
> )
> Unfortunately, the code to partially include/exclude some of the Predef stuff is ugly:
>
> import scala.Predef.{
> assert => _,
> assume => _,
> print => _,
> printf => _,
> println => _,
> require => _,
> _
> }
> import scala.Console
>
> object Main extends App {
> Console.out.println("Hello, World!")
> }
> If I could globally apply that nasty import to every .scala file, that'd be awesome to say the least.

Just write your own Predef, which contains only the definitions that you want. This way you only have to import your own Predef.

Marconi

unread,
Oct 27, 2016, 4:02:34 PM10/27/16
to scala-debate
Try

$ scalac -help
$ scalac -X
$ scalac -Y

You are going to discover quite a few goodies there.

Reply all
Reply to author
Forward
0 new messages