Scala Tutorial - Learn How To Create Functions As Symbols

By Nadim Bahadoor | Last updated: March 16, 2018 at 11:32 am

Overview

In this tutorial, we will learn how to create functions which are named using just symbols as opposed to alphabets. This feature of Scala is a great building block when you have to create Domain Specific Language syntax.

 

A great example would be the use of the ! symbol in the Akka Framework when sending messages to an Akka Actor.


myActor ! "Hello World"

Steps

1. How to create and instantiate a class

Let's start with creating a class named DonutCostCalculator and hard-coding a value for the total cost as 100. I'm sure in practice you will not be hard-coding the total cost value in your program :)


class DonutCostCalculator {

// We are hard-coding the totalCost value for simplicity.
val totalCost = 100
}

Next, let's add a function named minusDiscount() which will return the totalCost - some discount parameter.


def minusDiscount(discount: Double): Double = {
  totalCost - discount
}

Next, let's create an instance of the DonutCostCalculator as follows:


println("Step 1: How to create and instantiate a class")
val donutCostCalculator = new DonutCostCalculator()

 

2. How to call a function from an instantiated class

If you have used Java or .NET in the past, calling a function on an instantiated class should feel very familiar.

 

To call the minusDiscount() function, you simply need to call the function on the donutCostCalculator value.


println("\nStep 2: How to call a function from an instantiated class")
println(s"Calling minusDiscount() function = ${donutCostCalculator.minusDiscount(10.5)}")

NOTE:

  • Since the minusDiscount() function accepts a Double as its parameter, we are passing some random double of 10.5.

3. How to define function whose name is just the symbol minus  

Defining a function whose name is a symbol is essentially identical to defining any other functions. Instead of having a name, the function will be defined with some symbol.


// Step 3: How to define function whose name is just the symbol minus -
def -(discount: Double): Double = {
  totalCost - discount
}

NOTE:

  • We've defined a function called - where the name of function is simply the symbol - itself.

4. How to call function whose name is just a symbol

Calling a function whose name is just a symbol like the - function from Step 3 above is no different than how you would call any other functions.


println("\nStep 4: How to call function whose name is just the symbol -")
println(s"Calling function - = ${donutCostCalculator.-(10.5)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to call function whose name is just the symbol -
Calling function - = 89.5

 

5. How to call a function using the operator style notation

Scala allows you to call functions using the operator style notation as shown below:


println("\nStep 5: How to call a function using the operator style notation")
println(s"Calling function - with operator style notation = ${donutCostCalculator - 10.5}")

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to call a function using the operator style notation
Calling function - with operator style notation = 89.5

NOTE:

  • Calling a function using the operator style is not much different than calling a function except that you do not need to specify the .
  • Using operator style is more clear when calling functions whose names are just symbols.

6. How to define function whose name is just the symbols +++

Now that you know how to define functions whose names are just symbols, let us create another function named +++.


// Step 6: How to define function whose name is just the symbols +++
def +++(taxAmount: Double): Double = {
  totalCost + taxAmount
}

NOTE:

  • This is where it starts getting a bit trickier when defining functions with symbols.
  • Is +++ clear that we are adding some taxes to the totalCost? Probably not!
  • While using functions which are defined as symbols can be very powerful, try not to abuse this feature as shown here.

This concludes our tutorial on Learn How To Create Functions As Symbols and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to create and instantiate a class
  • How to call a function from an instantiated class
  • How to define function whose name is just the symbol minus
  • How to call function whose name is just a symbol
  • How to call a function using the operator style notation
  • How to define function whose name is just symbols +++

Tip

  • Defining functions using symbols is a great feature of Scala to allow you to create Domain Specific Language (DSL).
  • However, as shown in Step 6, make sure that the symbols you use are clear to the consumer of your API.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to define curried functions whose parameters are defined in groups.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: