Nicolas A Perez

@anicolaspp

Implicit conversions in Scala for C# developers

Implicits has been a topic that has caused troubles among many people I know learning Scala. Coming from .NET and C# it could be difficult to correlate the implicit term between the two languages.

C# implicit conversions

As the MSDN pages says

The implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.

Basically we are transforming a data type into a different one preserving the information and without using cast. For example, the next example shows how these transformations are made.

This particular feature improves code readability because the custom transformation is happening in a natural way, we avoid casting, while keeping the code clean enough.

Scala implicit conversions

In Scala, implicit conversions are basically the same as in C# with some syntax differences.

Here we just saw how the two languages solve the same conversion problem, but there is something else about Scala implicits that we can take advantage of and it is also what makes some developer coming from C# confused.

Type extensions

in C#, we have the ability to extend types without creating new derived types. We can avoid the recompilation process of the original type and its modification using the type extensions or method extensions construct of the language.

Lets see how this is done in C#

Here we are extending the type int and adding to it the method plus so we can do something like this:

This feature is very useful when it comes to extensibility and flexibility. Some APIs are really hard to consume because they weren’t intended to be used for others until they are and being able to extend their types gives us control over third party assemblies.

In Scala, we have this same functionality but as mentioned before, some people coming from the C# world have problem understand it. We could use the same implicits in Scala to solve the same problem that method extensions solve in C#.

Let’s suppose that we want to extend the Int type from Scala in the same way we did for C#. Here is how it goes.

Let’s clear that the plus method has the return type explicitly declared because it is public API method, so return types should be declared to avoid conflicts if the type inference systems in Scala changes.

In the last example, we have done the same we did in C#. We basically has define a way to extend the Int type, but in order to use it, we need to change types. In order to achieve this, we do:

Important to note that when returning the IntExtensions class we are omitting the new keyword because IntExtensions is a case class.

Now, we can use this extension in the same way we did in C#.

This use of the Scala implicits can cause a lot of problems when you have not seen it before. Probably the common use of changing types is something everyone can understand but catching this use case in Scala requires a deeper knowledge of the language when coming from other languages such as C#.

Notes in companion object in Scala

This is out from our topic, but I think is a good idea to talk about it since its importance in the Scala language. It will let everyone understand better my last examples where we completely ignored the new keyword when creating a IntExtensions instance.

There is a construct in Scala called companion object. It basically defines a convenient way to create new instances of a particular type.

Lets define again the IntExtensions class, but this time NOT as a case class.

The definition looks very close to the previous one except for the case keyword. Now in order to create a new instance of this class we need to do

every time. It would be nice if we could do just

instead. There is way to do it which is quite easy to implement. Let see how it gets done.

Here we are declaring an object with the same name of IntExtensions which has a function called apply and returns the actual IntExtensions instance. These two, the IntExtensions and its companion object need to be define together in order to work. Now we can do something like this.

This definition is the same as before, but remember that IntExtensions class is NOT a case class anymore, so we need the new keyword to create instance of it. What is happening is that the apply function from the IntExtensions companion object is being called when asking for an instance of it.

This particular feature let us write very clean code in Scala, especially when using common types across the system, an example of this is creating lists.

Instead of doing

Even though it seems dumb, when code gets complicated it does help a lot, the Python programming language is an example of it.

The C# language does not have this feature, yet, but there are tons of new features being added to the Microsoft programming language in order to catch up with the market.

I hope this explanation about implicit can help others coming from the Microsoft world to enjoy other programming languages in the same way I do.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

More by Nicolas A Perez

Topics of interest

More Related Stories