Creating Your Repo Module

Next we need to create our app’s repository module. We went through this process in detail in Chapter 1, ​Getting Started with Repo​ but we’ll run through it again here. The use Ecto.Repo directive…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Scala as Concise Java

Pragmatic Scala — by Venkat Subramaniam (23 / 140)

👈 Chapter 3 From Java to Scala | TOC | Scala Classes for Java Primitives 👉

Java code often has a lot of boilerplate code — getters, setters, access modifiers, code to deal with checked exceptions…. The growing list bloats the code. As you’ll see here, the Scala compiler walks a few extra miles so you don’t have to expend efforts to write and maintain code that can be generated.

Scala has very high code density — you type less to achieve more. To contrast, let’s start with an example of Java code:

Here’s the output:

Scala makes quite a few things in this code optional. First, it does not care whether you use semicolons. Second, there’s no real benefit for the code to live within the class Greetings in a simple example like this, so you can get rid of that. Third, there’s no need to specify the type of the variable i. Scala is smart enough to infer that i is an integer. Finally, you can use println without the System.out. prefix. Here’s the Java code simplified to Scala:

To run this script, type scala Greet.scala on the command line, or run it from within your IDE.

You should see this output:

Rather than concatenating the print message using the + operator, we used string interpolation (using the syntax s”…${expression}…”), which makes code more expressive and concise — we’ll discuss string interpolation in String Interpolation.

Scala’s loop structure is pretty lightweight. We simply indicated that the value of the index i goes from 1 to 3. The left of the arrow (<-) defines a val and its right side is a generator expression. On each iteration, a new val is created and initialized with a consecutive element from the generated values.

Add a comment

Related posts:

Calling next with Schedulers

Upgrade your skillset, succeed at work, and above all, avoid the many headaches that come with modern front-end development. Simplify your codebase with hands-on examples pulled from real-life applications. Master the mysteries of asynchronous state management, detangle puzzling race conditions, and send spaceships soaring throu

Chapter 4 Mobile Recipes

More and more people access websites and applications from mobile devices, and we need to develop with these users in mind. Limited bandwidth, smaller screens, and new user interface interactions…

Recovering From the Trauma of the Trump Administration

When I listen to the news, I still find myself ready to cringe. We’ve grown used to one attack, one shock after another, continuing assaults on our lives or humanity. It’s been such a relief since…