Showing posts from July, 2018

sealed keyword in Scala

sealed classes or traits can only be extended within the same file where it is defined. This is especially useful while creating the Algebraic Data Types.

Here are some of the characteristics of sealed traits

sealed traits should be extended within the same file so that the compiler knows all the possible subtypes otherwise the compiler can emit warnings if a match/case expression is not exhaustiveit cannot be extended anywhere else the same fileshould only be used if all the subtypes are early known in advance.this can be a way of creating something like an enum in Java

we can see the use of sealed traits in scala's Option like

sealedtraitOption[+A] finalcaseclassSome[+A] extendsOption[A] objectNoneextendsOption[Nothing]
so if we use these traits with pattern matching like this then we will get a compile error saying
Warning: match may not be exhaustive. It would fail on the following input: None

opt match { caseSome(a) =>"hello" }
and another benefit of using thi…

object keyword in scala

The object keyword in scala creates a new singleton type, similar to the singleton class in Java i.e. its instance cannot be created twice and only has a single named instance. It's just like creating a new instance of the anonymous class in Java. Scala doesn't have a static concept. So scala creates a singleton object to provide an entry point for your program execution. If you don't create a singleton object, your code will compile successfully but will not produce any output. Methods declared inside Singleton Object are accessible globally. A singleton object can extend classes and traits.

objectSingleton{ def main(args:Array[String]){ //no object creation is needed here SingletonObject.hello() } objectSingletonObject{ def hello(){ println("Hello, This is Singleton Object") } }
Hello, This is Singleton Object

In scala, when we have a class with the same name as a singleton object, it is called as a compani…

Akka Actors in Java - Part 1

Whenever we are dealing with the large and enterprise applications we always need to focus more on synchronizations and locks. This stuffs are more complex and also is time-consuming. But with Akka, we can easily write asynchronous code without the need for locks and synchronization. Akka is based on the message-driven terminology so that we don't need to wait for a return value instead the receiver will reply the sender with a message that will contain the result. And all actors are monitored by the parents so that it will also be easier to notify and stop the actors if the failure happens. In order to use Actors in our system lets first add akka-actor dependency in our pom.xml

//As we discussed all the actors will be in the hierarchy so lets create first the actor sys…