Showing posts with label Scala. Show all posts
Showing posts with label Scala. Show all posts

Tuesday, 8 January 2019

Scala 3.0 Features

Scala 3.0, an upgrade to the object-oriented, functional Scala language that started out on the JVM, is expected in early 2020, anchored by a next-generation compiler platform known as Dotty.

In fact, Dotty will become Scala 3.0, said Scala language founder Martin Odersky. Dotty has been centered on simplification, with extraneous syntax such as XML literals removed. Dotty also tries to slim down Scala types into a smaller set of fundamental constructs.

Planned new features in Scala 3

Objectives for the Scala 3 release include:

Becoming more opinionated by promoting idioms that have worked well.
Consolidation of constructs to improve consistency, ergonomics, performance and safety.
Simplicity where it can be implemented.
Ridding the language of inconsistencies and "surprising" behavior.
For tools support, the Scala 3 compiler, dotc, has been used to compile itself and a set of libraries. A REPL (read-eval-print loop) is supported by the compiler also. IDE support is provided by having dotc use the Language Server Protocol. Scala 3 support also will be available in the JetBrains IntelliJ IDE via a plugin.

How Scala 3 compares to Scala 2
The intent is to publish Scala 3.0 after Scala 2.14, which will feature migration to version 3.0, featuring tools, shim libraries, and targeted deprecations. Scala 2.13 is due in a few months, and Scala 2.14 will follow that likely in 2019.

Scala 2 and 3 are fundamentally the same, although the compiler is new, Odersky said. But Versions 2 and 3 are not binary-compatible. Version 3 can use Scala 2 artifacts, and both versions share the same standard library. You can also cross-build code for Scala 3 and 2; a guide will define a shared language subset to be compiled under both releases. The -language:Scala2 option in the Scala 3 compiler lets it compile most Scala 2 code.

Thank you for reading my tutorials.

Happy Learning!

Friday, 11 May 2018

Scala Naming Conventions

NAMING CONVENTIONS

Generally speaking, Scala uses “camel case” naming. That is, each word is capitalized, except possibly the first word:
UpperCamelCase
lowerCamelCase
Underscores in names (_) are not actually forbidden by the compiler, but are strongly discouraged as they have special meaning within the Scala syntax. (But see below for exceptions.)

Classes/Traits

Classes should be named in upper camel case:
class MyFairLady
This mimics the Java naming convention for classes.

Objects

Object names are like class names (upper camel case).
An exception is when mimicking a package or function. This isn’t common. Example:
object ast {
  sealed trait Expr

  case class Plus(e1: Expr, e2: Expr) extends Expr
  ...
}

object inc {
  def apply(x: Int): Int = x + 1
}

Packages

Scala packages should follow the Java package naming conventions:
// wrong!
package coolness

// right! puts only coolness._ in scope
package com.novell.coolness

// right! puts both novell._ and coolness._ in scope
package com.novell
package coolness

// right, for package object com.novell.coolness
package com.novell
/**
 * Provides classes related to coolness
 */
package object coolness {
}

root

It is occasionally necessary to fully-qualify imports using _root_. For example if another net is in scope, then to access net.liftweb we must write e.g.:
import _root_.net.liftweb._
Do not overuse _root_. In general, nested package resolves are a good thing and very helpful in reducing import clutter. Using _root_ not only negates their benefit, but also introduces extra clutter in and of itself.

Methods

Textual (alphabetic) names for methods should be in lower camel case:
def myFairMethod = ...
This section is not a comprehensive guide to idiomatic method naming in Scala. Further information may be found in the method invocation section.

Accessors/Mutators

Scala does not follow the Java convention of prepending set/get to mutator and accessor methods (respectively). Instead, the following conventions are used:
  • For accessors of properties, the name of the method should be the name of the property.
  • In some instances, it is acceptable to prepend “`is`” on a boolean accessor (e.g. isEmpty). This should only be the case when no corresponding mutator is provided. Please note that the Lift convention of appending “_?” to boolean accessors is non-standard and not used outside of the Lift framework.
  • For mutators, the name of the method should be the name of the property with “_=” appended. As long as a corresponding accessor with that particular property name is defined on the enclosing type, this convention will enable a call-site mutation syntax which mirrors assignment. Note that this is not just a convention but a requirement of the language.
    class Foo {
    
      def bar = ...
    
      def bar_=(bar: Bar) {
        ...
      }
    
      def isBaz = ...
    }
    
    val foo = new Foo
    foo.bar             // accessor
    foo.bar = bar2      // mutator
    foo.isBaz           // boolean property
    
Unfortunately, these conventions fall afoul of the Java convention to name the private fields encapsulated by accessors and mutators according to the property they represent. For example:
public class Company {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
In Scala, there is no distinction between fields and methods. In fact, fields are completely named and controlled by the compiler. If we wanted to adopt the Java convention of bean getters/setters in Scala, this is a rather simple encoding:
class Company {
  private var _name: String = _

  def name = _name

  def name_=(name: String) {
    _name = name
  }
}
While Hungarian notation is terribly ugly, it does have the advantage of disambiguating the _name variable without cluttering the identifier. The underscore is in the prefix position rather than the suffix to avoid any danger of mistakenly typing name _ instead of name_. With heavy use of Scala’s type inference, such a mistake could potentially lead to a very confusing error.
Note that the Java getter/setter paradigm was often used to work around a lack of first class support for Properties and bindings. In Scala, there are libraries that support properties and bindings. The convention is to use an immutable reference to a property class that contains its own getter and setter. For example:
class Company {
  val string: Property[String] = Property("Initial Value")

Parentheses

Unlike Ruby, Scala attaches significance to whether or not a method is declaredwith parentheses (only applicable to methods of arity-0). For example:
def foo1() = ...

def foo2 = ...
These are different methods at compile-time. While foo1 can be called with or without the parentheses, foo2 may not be called with parentheses.
Thus, it is actually quite important that proper guidelines be observed regarding when it is appropriate to declare a method without parentheses and when it is not.
Methods which act as accessors of any sort (either encapsulating a field or a logical property) should be declared without parentheses except if they have side effects. While Ruby and Lift use a ! to indicate this, the usage of parens is preferred (please note that fluid APIs and internal domain-specific languages have a tendency to break the guidelines given below for the sake of syntax. Such exceptions should not be considered a violation so much as a time when these rules do not apply. In a DSL, syntax should be paramount over convention).
Further, the callsite should follow the declaration; if declared with parentheses, call with parentheses. While there is temptation to save a few characters, if you follow this guideline, your code will be much more readable and maintainable.
// doesn't change state, call as birthdate
def birthdate = firstName

// updates our internal state, call as age()
def age() = {
  _age = updateAge(birthdate)
  _age
}

Symbolic Method Names

Avoid! Despite the degree to which Scala facilitates this area of API design, the definition of methods with symbolic names should not be undertaken lightly, particularly when the symbols itself are non-standard (for example, >>#>>). As a general rule, symbolic method names have two valid use-cases:
  • Domain-specific languages (e.g. actor1 ! Msg)
  • Logically mathematical operations (e.g. a + b or c :: d)
In the former case, symbolic method names may be used with impunity so long as the syntax is actually beneficial. However, in the course of standard API design, symbolic method names should be strictly reserved for purely-functional operations. Thus, it is acceptable to define a >>= method for joining two monads, but it is not acceptable to define a << method for writing to an output stream. The former is mathematically well-defined and side-effect free, while the latter is neither of these.
As a general rule, symbolic method names should be well-understood and self documenting in nature. The rule of thumb is as follows: if you need to explain what the method does, then it should have a real, descriptive name rather than a symbols. There are some very rare cases where it is acceptable to invent new symbolic method names. Odds are, your API is not one of those cases!
The definition of methods with symbolic names should be considered an advanced feature in Scala, to be used only by those most well-versed in its pitfalls. Without care, excessive use of symbolic method names can easily transform even the simplest code into symbolic soup.

Constants, Values, Variable and Methods

Constant names should be in upper camel case. Similar to Java’s static finalmembers, if the member is final, immutable and it belongs to a package object or an object, it may be considered a constant:
object Container {
  val MyConstant = ...
}
The value: Pi in scala.math package is another example of such a constant.
Method, Value and variable names should be in lower camel case:
val myValue = ...
def myMethod = ...
var myVariable

Type Parameters (generics)

For simple type parameters, a single upper-case letter (from the English alphabet) should be used, starting with A (this is different than the Java convention of starting with T). For example:
class List[A] {
  def map[B](f: A => B): List[B] = ...
}
If the type parameter has a more specific meaning, a descriptive name should be used, following the class naming conventions (as opposed to an all-uppercase style):
// Right
class Map[Key, Value] {
  def get(key: Key): Value
  def put(key: Key, value: Value): Unit
}

// Wrong; don't use all-caps
class Map[KEY, VALUE] {
  def get(key: KEY): VALUE
  def put(key: KEY, value: VALUE): Unit
}
If the scope of the type parameter is small enough, a mnemonic can be used in place of a longer, descriptive name:
class Map[K, V] {
  def get(key: K): V
  def put(key: K, value: V): Unit
}

Higher-Kinds and Parameterized Type parameters

Higher-kinds are theoretically no different from regular type parameters (except that their kind is at least *=>* rather than simply *). The naming conventions are generally similar, however it is preferred to use a descriptive name rather than a single letter, for clarity:
class HigherOrderMap[Key[_], Value[_]] { ... }
The single letter form is (sometimes) acceptable for fundamental concepts used throughout a codebase, such as F[_] for Functor and M[_] for Monad.
In such cases, the fundamental concept should be something well known and understood to the team, or have tertiary evidence, such as the following:
def doSomething[M[_]: Monad](m: M[Int]) = ...
Here, the type bound : Monad offers the necessary evidence to inform the reader that M[_] is the type of the Monad.

Annotations

Annotations, such as @volatile should be in lower camel case:
class cloneable extends StaticAnnotation
This convention is used throughout the Scala library, even though it is not consistent with Java annotation naming.
Note: This convention applied even when using type aliases on annotations. For example, when using JDBC:
type id = javax.persistence.Id @annotation.target.field
@id
var id: Int = 0

Special Note on Brevity

Because of Scala’s roots in the functional languages, it is quite normal for local names to be very short:
def add(a: Int, b: Int) = a + b
This would be bad practice in languages like Java, but it is good practice in Scala. This convention works because properly-written Scala methods are quite short, only spanning a single expression and rarely going beyond a few lines. Few local names are used (including parameters), and so there is no need to contrive long, descriptive names. This convention substantially improves the brevity of most Scala sources. This in turn improves readability, as most expressions fit in one line and the arguments to methods have descriptive type names.
This convention only applies to parameters of very simple methods (and local fields for very simply classes); everything in the public interface should be descriptive. Also note that the names of arguments are now part of the public API of a class, since users can use named parameters in method calls.

Thursday, 2 November 2017

Play 2.6.x: Joda DateTime Format not working

In Play Framework 2.6.x, Joda DateTime Format (that is READs and WRITEs) is not working. When we use something like below:

(JsPath \ "joiningDate").read[LocalDate] 

We will see the following error message:

No Json deserializer found for type org.joda.time.LocalDate. Try to implement an implicit Reads or Format for this type.

To fix that issue, we need to do the following two setps:

1. Add the following entry into your build.sbt file

libraryDependencies += "com.typesafe.play" % "play-json-joda_2.12" % "2.6.6"

Or

scalaVersion := "2.12.2"
libraryDependencies += "com.typesafe.play" %% "play-json-joda" % "2.6.6"

2. Add the following imports to your Model file
import play.api.libs.json.JodaWrites._
import play.api.libs.json.JodaReads._

Description:
They have separated the Joda Date and Time library into a separate module:
play-json-joda

That's it.

Thank you for reading my tutorials.

Wednesday, 17 May 2017

Activator will be EOL-ed on May 24, 2017.

Activator will be EOL-ed on May 24, 2017.

We’re making it easier and simpler for developers to get started with Lightbend technologies.


This unfortunately means that future releases of Play, Akka and Scala will no longer include Activator support, and Lightbend’s Activator server will be decommissioned by the end of 2017. Instead of supporting Activator to create and set up development projects, we'll be supporting standard Giter8 templates for sbt users and Maven archetypes for Maven users.

So going forward,
To create new Lightbend projects
Instead of using the Activator command, make sure you have sbt 0.13.13 (or higher), and use the “sbt new” command, providing the name of the template.

For example, “$ sbt new akka/hello-akka.g8”. You can find a list of templates here.

Also, as a convenience, the Lightbend Project Starter allows you to quickly create a variety of example projects that you just unzip and run.

To create new templates

If you want to create new templates, you can now do that in Giter8.

To migrate templates from Activator to Giter8
If you created Activator templates in the past, please consider migrating them to Giter8 with this simple process.

Thank you!

Wednesday, 14 September 2016

Scala Partial Functions In Depth

Post Brief Table of Content

  • Introduction
  • What is Partial Function?
  • Partial Function General Example
  • PartialFunction in Scala API
  • Partial Function Examples
  • Partial Function Rules
  • Partial Function Real-Time Scenarios
  • Scala Partial Functions Interview Questions

Introduction

Before reading this post, please go through my previous Scala posts to learn some Scala Basics. In this post, I'm going to discuss one interesting concept: Scala Partial Functions.

Scala Language has many kinds of functions. Partial Function is one of the types of Functions available in Scala Language. To read about functions basics, please click here: Scala Functions Basics

What is Partial Function?

If a function does NOT support all argument(s) of it's input(s), then that function is known as a Partial Function. Opposite to Partial Function is Total Function which supports all argument(s) of it's input(s).

Partial Functions are defined only partially, which does not support all possible input(s). It supports only subset of it's input(s).

In Scala, we can use scala.PartialFunction trait to define Partial Functions. We will discuss how to define it with some examples in the coming sections.


Partial Function General Example

In this section, we will take one Real-time Example to explain this Partial Function concept.

I think, everyone did some Mathematics from Schooling days.  In Maths, division operation does not support for all inputs. It is NOT defined for denominator = 0. Hence, division is a partial function.

In a/b, if b=0 we will get ArthimeticException 
scala> 1/0
java.lang.ArithmeticException: / by zero
Here nominator = 1 and denominator = 0. Hence we got exception.

Scala Function Example:-
scala> def division(no1:Int, no2:Int) = no1/no2
division: (no1: Int, no2: Int)Int

scala> division(1,1)
res9: Int = 1

scala> division(1,0)
java.lang.ArithmeticException: / by zero

scala> division(0,1)
res11: Int = 0

scala> division(0,0)
java.lang.ArithmeticException: / by zero

PartialFunction In Scala API

Scala has built-in support to define Partial functions. We can use scala.PartialFunction trait to define Partial Functions.

Let us explore scala.PartialFunction trait now. In Scala API, it is define as shown below:

package scala
trait PartialFunction[-A, +B] extends (A) ⇒ B
In simple way, it is defined as PartialFunction[A,B]
  • A is input to this function
  • B is output to this function
It has couple of functions. "isDefinedAt" is more popular and useful function. We will discuss about this function usage in the Examples section.

Partial Function Examples

Let us explore PartilaFunction with some examples here.

Example-1:-
scala> val increment: PartialFunction[Int, Int] = {
     |  case x:Int => x+1
     | }
increment: PartialFunction[Int,Int] = <function1>

scala> increment(10)
res22: Int = 11

Here both input and output are of same type: Int.

Example-2:-
Now let us define our division function (defined "Partial Function Real-time Example" section) using PartialFunction

scala> val division: PartialFunction[(Int,Int), Int] = {
     |  case (n:Int,d:Int) if d!= 0 => n/d
     | }
division: PartialFunction[(Int, Int),Int] = <function1>

scala> division(1,1)
res23: Int = 1

scala> division(1,0)
scala.MatchError: (1,0) (of class scala.Tuple2$mcII$sp)

scala> division(0,1)
res25: Int = 0

scala> division(0,0)
scala.MatchError: (0,0) (of class scala.Tuple2$mcII$sp)

Here input is a Tuple2 of (Int,Int) type and output is of Int type.

Example-3:-

scala> val greet: PartialFunction[String, Unit] = 
       { case name:String => println("Hello, "+ name) }
greet: PartialFunction[String,Unit] = <function1>

scala> greet("Rams")
Hello, Rams

A Partial Function can return Unit (no return value) also.

Partial Function Rules


In Scala Language, we should follow some rules to define Partial Functions:
  • Use scala.PartialFunction trait to define partial functions
  • "case" block(s) are used to define partial function's body
  • Entire function should be enclosed in curly braces

scala> val increment: PartialFunction[Int, Int] = case x:Int => x+1
<console>:1: error: illegal start of simple expression
val increment: PartialFunction[Int, Int] = case x:Int => x+1

Here we are using same increment function but without curly braces.
  • We can use "isDefinedAt" to check whether a Partial Function is defined at given input

if(division.isDefinedAt(1,0))
// Do something
else
// Do something else


NOTE:-
If input does NOT any available cases in a Partial Function, it throws MatchError as shown below:
scala> val division: PartialFunction[(Int,Int), Int] = {
     |  case (n:Int,d:Int) if d!= 0 => n/d
     | }
division: PartialFunction[(Int, Int),Int] = <function1>

scala> division(0,0)
scala.MatchError: (0,0) (of class scala.Tuple2$mcII$sp)
Partial Function Real-time Scenarios
We have already discussed some useful examples in above sections. However, I want to discuss some real-time scenarios in this section

Scenario-1:- 

One of the best examples is Akka Actors
def receive {

  case ... => Do some thing
}
In developing Actors, we should define the implementation of a receive function as shown in the above example. It is actual a Partial Function

def receive: PartialFunction[Any,Unit] = { ... }
Scenario-2:- 

Scala API uses Partial Function concept in many places. For instance, collect function available in Collection API.


Scenario-3:- 

We can use Partial Function where we don't have full definition for it's inputs.

Partial Function Interview Questions

That’s In this section, we will discuss some useful Scala Partial Functions related Interview Questions and Answers:

  • What is Partial Function?
  • What is Total Function?
  • What is the type of a Scala Partial Function?
         In Scala, the type of a  Partial Function is scala.PartialFunction[A,B]
  • When do we get a MatchError in a Partial Function?

That’s it all about “Scala Partial Functions”. We will discuss some more important Scala Concepts in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.