An Intro to Scala and Sending Email in the JVM via SendGrid

June 18, 2014
Written by
Opinions expressed by Twilio contributors are their own

An Intro to Scala and Sending Email in the JVM via SendGrid

SendGrid and the Java Virtual Marketplace

Are you a Java developer looking to use the Java Virtual Machine (JVM) in different ways like Clojure? If you're looking into Scala, then this post might be able to help you out.

Scala runs in the JVM. If you weren't aware, all of your Java code can run in Scala. Pretty awesome, right? For example, the SendGrid JAR recently went through some major changes in order to make it more robust. You can use the old JAR as well as the new one because in the end, it all becomes bytecode for the JVM.

Differences Between Java and Scala

Let's learn a bit about the difference in syntaxes when coming from Java and using the SendGrid JAR in order to, you know, send email.

Importing the library in Java is as simple as:

Its Scala counter part is:

Basically _ implies wildcard, while in Java it's *. Also, in Scala, there is no need for semicolons. They're completely optional.

Instantiating SendGrid's objects is quite similar. The one thing that changes is the fact that Scala has Type inference and there is no need to specify the type of the object. Check out the difference between the two:



The rest is the same. If you want an Inmutable object, instantiate it using the keyword val.

Another cool thing about Scala is that you can invoke methods using space instead of the usual dot notation. For example:

They're completely equal.

This is definitely not a comprehensive beginner guide, but I'd be super stoked to find out that it got you a bit interested in Scala. If so, give me a shout out @elbuo8 :)

Big shout out to Jesus Luzon for the Scala class!

Recommended For You

Most Popular

Send With Confidence

Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.