My company had an internal hack-a-thon and the night before the presentations, I was forced to take some JSON from an API and render it on a page. Complete unfamiliarity compounded with a time restraints had me completely unable to do it. So this is me fixing that for next time:

JS Object

There are a lot of ways to handle JSON but if you’re using the Play framework, you can handle it natively. Just import the play.api.libs.json and you gain access to the JsValue trait which has a ton of case classes for nearly any type. Some examples include JsString, JsNumber…


Sorry, considering the other blog posts I did on apply and unapply as well as companion objects, this one regarding case classes seems a bit out of order. Nevertheless, we should talk about case classes which are special to Scala. Case classes bring class instantiation [creation] and definition into the modern world. Consider the following code:

class Book(isbn: String) {
def apply(isbn: String) = {
val b = new Book
b.isbn = isbn
b
}
def unapply(b: Book): String = s"${b.isbn}"
}
val frankenstein = new Book("978-0486282114")

This is how we would normally would define a class for a new…


If you’re unfamiliar with Scala then you’ll likely come across and be confused by some code that looks like the following:

case class Rectangle (length: Int, width: Int)object Rectangle {
def area(rectangle: Rectangle) = rectangle.length * rectangle.width
}
val r1 = Rectangle(3, 7)Rectangle.area(r1) //returns 21

Why do we have a class and object of the same name created together in the same file? The answer here is simple. In Scala, you can think of classes and case classes as the data storage, while its companion object is where you should add class methods and constructors.

While it is…


In Scala, the apply and unapply methods are pretty important. Some classes have these built-in automatically [I’m looking at you, ‘case class’], while in others they need to be explicitly written. This post is going to talk about what these two very important methods are and what they do.

APPLY

The apply method ties the parameters to the actual instance of the class or case class. Below is an example:

class Person {
var name: Option[String] = None
var age: Option[Int] = None
}
object Person { def apply = (name: Option[String], age:Option[Int]): Person = { var p = New Person…

Micah Lewis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store