How do I cast a variable in Scala?

The preferred technique is to use pattern matching. This allows you to gracefully handle the case that the value in question is not of the given type: g match { case g2: Graphics2D => g2 case _ => throw new ClassCastException } This block replicates the semantics of the asInstanceOf[Graphics2D] method, but with greater flexibility. … Read more

What is the difference between JavaConverters and JavaConversions in Scala?

EDIT: Java Conversions got @deprecated in Scala 2.13.0. Use scala.jdk.CollectionConverters instead. JavaConversions provide a series of implicit methods that convert between a Java collection and the closest corresponding Scala collection, and vice versa. This is done by creating wrappers that implement either the Scala interface and forward the calls to the underlying Java collection, or … Read more

What do

These are called generalized type constraints. They allow you, from within a type-parameterized class or trait, to further constrain one of its type parameters. Here’s an example: case class Foo[A](a:A) { // ‘A’ can be substituted with any type // getStringLength can only be used if this is a Foo[String] def getStringLength(implicit evidence: A =:= … Read more

When should I choose Vector in Scala?

As a general rule, default to using Vector. It’s faster than List for almost everything and more memory-efficient for larger-than-trivial sized sequences. See this documentation of the relative performance of Vector compared to the other collections. There are some downsides to going with Vector. Specifically: Updates at the head are slower than List (though not … Read more

difference between foldLeft and reduceLeft in Scala

Few things to mention here, before giving the actual answer: Your question doesn’t have anything to do with left, it’s rather about the difference between reducing and folding The difference is not the implementation at all, just look at the signatures. The question doesn’t have anything to do with Scala in particular, it’s rather about … Read more

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