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

Basic Scala Implicit

Posted on: February 20, 2014

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…. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog Stats

  • 8,200 hits

Top Clicks

  • None


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3 other followers

%d bloggers like this: