J2EE Enterprise Design Patterns | how to configure web.xml (Or Deployment descriptor) | Java Application using MyEclipse Debugging | Scala Language

Archive for the ‘Uncategorized’ Category

Since last couple of days I was chasing Scala Language Implicit Conversions and implicit Parameters. When I was started to learn Play Framework, I have found there are many places in Controllers Action methods taking implicit request as input parameter and returning response based on executing some logic on implicit request.  During my leaning I was found it was not easy to get an idea about implicit Conversions and Parameters, so I thought to share my knowledge which explains you about some of it concept with examples and different cases.

If you have past experience on C, C++, Java or some programing language you guys have already used this feature as part of respective compiler implementation but you were not aware of it because Scala is the language which provide implicit as language feature. In case of implicit conversion compiler “Do the right thing for you” and convert lower level type to upper level whenever it’s required. Let’s understand this with example.

In salesforce we are using,

System.debug(‘Hello Salesforce  ‘ + 1);                  and                        Println(‘Hello Salesforce  ‘ + 1.0);

Same as in Java we are using somewhere in codes like,

System.out.println(“……  Hello Java” + 1.8);

If you see System.out.println taking two different types of parameters, one having String type and another having value of Integer type.

Have you ever thought of it, why it has happened and why java compiler has not given compilation error? Here comes implicit comes into picture. Behind the hood compiler will insert such convention for you, if compiler will not provide you such facility it would have given you compiler error while you trying to print using two different types for expression together.

Now let’s try to understand how these languages sometime accept different types of expressions as method argument and not showing compilation error. So when java compiler will see System.out.printl() with different expression, compiler will look for respective conversion method for the same scope and if it will find respective method, it will convert to appropriate type. So java has already done the job for you by implementing below method, and we can call these types of methods to run-time conversion methods.

public String int2String(int i) {
return String.valueOf(i);

So far so good, I guess now you have got your answer. When compiler saw System.out.println(“Hello Java” + 1) it will insert below convention for you,

System.out.println(“Hello Java” + int2String(1));

The same way it has happened for float2String, boolean2String etc……. The example you have seen above is called implicit conversion and understand how different languages using implicit conversion as part of their implementation.

Same way java 1.5 having concept of AutoBoxing and UnBoxing. These are also an example of implicit conversion. Java collection is collection of objects. When you declare list of integer

List list = new List();
list.add(1); //woking fine with JDK 1.5 or later

Above statement will work fine if you are using JDK 1.5 or later, prior JDK1.5 you have to write like,

list.add(new Integer(1));

Next part I will show you how Scala providing implicit as language feature and provide developer to freedom to use it in project implementation. I feel that Scala implicit must have to understand all Scala developer who all wants to write expressive code, which is easy to readable and execute complex logic behind the lid.

Hope this will help you. Please feel free to provide your feedback…. 🙂

Tuple in Scala

Before start with Scala tuple we go through what is Tuple in general term. In mathematical terms Tuple is list of ordered and unordered elements. For example if you say n-tuple, your tuple contains n numbers of elements as your tuple values. Scala tuple also having for the same purpose and added powerful feature to the Scala language.  As I mentioned my previous blog Scala emphasise functional programing language and they force to provide immutable collections and in Scala tuples are immutable like List and only different between list and tuples, tuples contains collection of different type.

  • Declare tuple in Scala

Define Scala Tuple

You can create tuple using above image, either writing simple list of elements or by creating object using new keyword. In above tuple I have created 3 element size tuple and while creating tuple I have used “new Tuple3” and in that 3 signify 3 elements size tuple. Scala gives you facility to create up to 22 size elements tuple using “new Tuple22” and if you try to create tuple with size more than 22 elements compiler gives you error like “error: not found: type Tuple”. If you see above image you will notice Scala automatically identify the type of tuple elements how you provide as tuple value. Now you see how to declare tuple but yes you have a question why we need the tuples, let’s take an example where you need your method return multiple values and in that case in java you have to declare a POJO class and return as part of your method return type. Now in Scala you can return tuple instead of your POJO class and you can do this simple thing by creating just tuple.

  • Access tuple elements in Scala

Once you declare elements in tuple you can access it using dot, underscore and element index. For detail see below image,

Access Scala Tuple

So as in above image to access 1st element in tuple I have used tuple._1 which will return first element of the define tuple and same for others what I have to do is change in index number and I will have indexed tuple value.

Now if you aware of Scala or java collection you might having question why we have to use dot, underscore and tuple element index to access the values from tuple. Now in Scala when you write tuple(1) Scala complier call apply method and if you see Scala API apply method always return same type of elements and as I said earlier, Scala tuple is collection of different type of elements.

Some Basic about Scala.