Using scala.util.control.Exception

Indeed – I also find it pretty confusing! Here’s a problem where I have some property which may be a parseable date: def parse(s: String) : Date = new SimpleDateFormat(“yyyy-MM-dd”).parse(s) def parseDate = parse(System.getProperty(“foo.bar”)) type PE = ParseException import scala.util.control.Exception._ val d1 = try { parseDate } catch { case e: PE => new Date … Read more

What’s the new way to iterate over a Java Map in Scala 2.8.0?

In 2.8, you import scala.collection.JavaConversions._ and use as a Scala map. Here’s an example (in 2.8.0.RC1): scala> val jmap:java.util.Map[String,String] = new java.util.HashMap[String,String] jmap: java.util.Map[String,String] = {} scala> jmap.put(“Hi”,”there”) res0: String = null scala> jmap.put(“So”,”long”) res1: String = null scala> jmap.put(“Never”,”mind”) res2: String = null scala> import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala> jmap.foreach(kv => println(kv._1 + ” … Read more

When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

The problem is that GenericTraversableTemplate is used twice: once for mutable collections (where its type parameter should be invariant), and once for immutable collections (where covariance is invariably king). GenericTraversableTemplate’s typechecks assuming either covariance or invariance for the A type parameter. However, when we inherit it in a mutable trait, we have to pick invariance. … Read more

Which IDE for Scala 2.8? [closed]

I’ve been pretty successful with IDEA 9. I’ve briefly tried both Netbeans and Eclipse and wasn’t able to get what I wanted. Eclipse’s code-complete didn’t behave as well as I’d have liked, and I couldn’t find a way to make Netbeans handle Scala scripts; It’d just complain that the file wasn’t a class. To be … Read more

How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

I have encountered the same scalac error when trying to run a Scala project in Intellij Idea 9.0.2 and I’ve managed to find a solution by chance :). These are the steps I took in creating the project and running it. I have created a Scala project in Intellij Idea 9.0.2 final (it was released … Read more

How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map?

Since 2.8 Scala has had .toMap, so: val map = seq.map(a => a.key -> a).toMap or if you’re gung ho about avoiding constructing an intermediate sequence of tuples, then in Scala 2.8 through 2.12: val map: Map[Int, A] = seq.map(a => a.key -> a)(collection.breakOut) or in Scala 2.13 and 3 (which don’t have breakOut, but … Read more

What’s the right way to use scala.io.Source?

Starting Scala 2.13, the standard library provides a dedicated resource management utility: Using. It can be used in this case with scala.io.Source as it extends AutoCloseable in order to read from a file and, no matter what, close the file resource afterwards: import scala.util.Using import scala.io.Source Using(Source.fromFile(“file.txt”)) { source => source.mkString } // scala.util.Try[String] = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)