Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Friday, November 13, 2020

Finally, C# 9 record, the equivalent of Scala's case class

While C# is a wonderful programming language, there is something that I would like to see to make our life programmer easier. If you are familiar with the Scala programming language you would know about "case class". 

Object Comparison

The case class in Scala is designed to support immutable data modeling. It provides object comparison out of the box without you need to compare each property in the object. For example in C# you have a Person class as below:

Tuesday, June 16, 2020

SBT Troubleshoot: Error on SBT Scala Compilation Because of Incompatible Java Version

Have you ever getting this kind of problem when trying to run your SBT command to compile your Scala code?


$ sbt compile
[info] Done updating.
error: error while loading String, class file '/modules/java.base/java/lang/String.class' is broken
(class java.lang.NullPointerException/null)
[error] java.io.IOError: java.lang.RuntimeException: /packages cannot be represented as URI
[error]         at java.base/jdk.internal.jrtfs.JrtPath.toUri(JrtPath.java:176)
[error]         at scala.tools.nsc.classpath.JrtClassPath.asURLs(DirectoryClassPath.scala:204)




Usually, this problem happens the first time you set up your project on your computer. This happens because you're using an incompatible Java version to compile your scala code. You can check the scala to Java compatibility in this page https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html

Now, how can you fix this? You need to find out which Java version is being used by SBT and then set the correct one to use. Do you try to use java -version command?

$ java -version



While the java -version command helps you telling which java is being used by your system, SBT might use a different version. For SBT specific, you can use sbt -J-showversion command as follow:

$ sbt -J-showversion
openjdk version "1.8.0_192"
OpenJDK Runtime Environment (Zulu 8.33.0.1-macosx) (build 1.8.0_192-b01)
OpenJDK 64-Bit Server VM (Zulu 8.33.0.1-macosx) (build 25.192-b01, mixed mode)



After knowing your Scala compatibility and your SBT's Java version you can follow any of these solutions in this page https://users.scala-lang.org/t/telling-sbt-to-use-different-jdk-version/4608/10

I hope this helps anyone having a similar problem.

Wednesday, March 11, 2020

Tips: Dealing with joda time LocalDate comparison in scala using implicit class

If you are coming from C#, working with DateTime is pretty straight forward. You can compare equal, greater than, less than, or getting the day difference from two dates without any hassle. But if you are in Scala, using joda time, it's not easy to get it right at the right for the first time. the LocalData has several functions can be used to compare the date-time. They are isAfter, isEqual, isBefore, and daysBeetween to get the diff of the day.

Here is my favorite approach to deal with joda time LocalDate comparison. First, we need to create an implicit class. In C#, this is comparable to the Extension Method. We can add or extend the functionality of a class without modifying the class implementation. In this case, we will extend LocalData as below:

----------------------
import org.joda.time.{Days, LocalDate}
object LocalDateExtension {   class LocalDateExtension(a: LocalDate) {     def dayDiff(b: LocalDate): Int = {       Days.daysBetween(a, b).getDays.abs     }
    def >(b: LocalDate): Boolean = {       a.isAfter(b)     }
    def >=(b: LocalDate): Boolean = {       a.isAfter(b) || a.isEqual(b)     }
    def <(b: LocalDate): Boolean = {       a.isBefore(b)     }
    def <=(b: LocalDate): Boolean = {       a.isBefore(b) || a.isEqual(b)     }   }
  implicit def extendLocalDate(a: LocalDate) = new LocalDateExtension(a) }
------------------------

What we are doing here? In scala, we are allowed to name a method using any character such as equal, greater than, or less than signs. This way, we can use the same syntax we use in C# to compare dates. For example as below:

----------------------
import org.joda.time.{Days, LocalDate}
val a = LocalDate("2010-04-28") val b = LocalDate("2010-04-29")
print(a > b) // false print(a >= b) // false print(a < b) // true print(a <= b) // true
------------------------


Finally, C# 9 record, the equivalent of Scala's case class

While C# is a wonderful programming language, there is something that I would like to see to make our life programmer easier. If you are fam...