Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Professional Scala

You're reading from   Professional Scala Combine object-oriented and functional programming to build high-performance applications

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher
ISBN-13 9781789533835
Length 186 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
 Hartmann Hartmann
Author Profile Icon Hartmann
Hartmann
 Shevchenko Shevchenko
Author Profile Icon Shevchenko
Shevchenko
Arrow right icon
View More author details
Toc

Language Features for Writing DSLs


In this section, we'll look at the Scala features that make it easy to write small DSLs:

  • Flexible syntax for method invocation

  • By-name parameters

  • Extension methods and Value classes

We'll use all of these features in the next section when we create our own DSL in Scala.

Flexible Syntax for Method Invocation

Scala has a flexible syntax for method invocations that makes it possible to, in some cases, omit the dot ( .) and parentheses ( ()) when invoking methods.

The rules are as follows:

  • For methods that are of arity- 0, meaning they don't take any parameters, the parentheses can be omitted and you can use the postfix notation.

  • For methods with an arity of 1 or more, meaning they take one or more parameters, it's possible to write the method using infix notation.

Here's an example of using infix notation when invoking filter:

List.range(0, 10).filter(_ > 5)
List.range(0, 10) filter (_ > 5)

And here's an example of omitting the parentheses when invoking...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images