Scala, Java, Unix, MacOS tutorials (page 56)

Somewhere in the Erie/Longmont area yesterday (November 2, 2020), I caught this photo of a hawk flying along a farm, with the Rocky Mountains in the background. As you can see, the sky was perfectly clear yesterday, which is a nice change after all of the fires this summer.

A hawk, a farm, and the Rocky Mountains

“Fainting does feel as if I’m practicing dying. Already I know how abruptly the world withdraws even as people call out your name, how darkness and confusion shutter your vision and bewildering images crowd your brainpan, how you cannot simply will yourself to return once you’ve entered that shadowy place.”

From this story about Sherry Simpson, “brilliant writer, beloved friend and mentor to a whole generation of Alaska writers, passed away unexpectedly earlier this month after a brief illness.”

If you’re going to have an obituary, “She changed my life” is a darned good one.

“Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.”

~ Patrick McKenzie (as seen on this tweet)

I had a problem with a Scala Future example a couple of days ago, and Guillaume Martres helped me through it. He also mentioned one thing about Scala futures that I thought I should share here:

“Rule of thumb is: never run futures in the constructor of an object, this can easily lead to deadlock as you try calling a method of the object while the object is being initialized ... moving everything inside a main method is fine.”

I’ve seen that type of comment in other discussions related to the time required to create constructors, and when I saw what he wrote there it really hit home.

Here’s a photo of the building I grew up in, in Chicago, Illinois. (Thanks, Google Maps.)

My old Chicago home

Here’s a side view of the building I grew up in, in Chicago, Illinois. (Thanks, Google Maps.) I used to throw rubber baseballs and tennis balls against that garage for hours, which is probably one reason my shoulder was screwed up before I left high school.

My old Chicago home (side view)

I was just reminded that the “object MyApp extends App” syntax is basically deprecated in Dotty. The correct approach in Scala 3 is to use an @main method, or to manually define a main method.

In my case I was trying to run an old Scala 2 futures example with Dotty without updating the App syntax, and ran into a problem. Once I switched to using the @main method everything worked as expected.

Many thanks to Guillaume Martres for his help!

As a brief note to self, if you need to run a main method in a JAR file created with Dotty (Scala 3), and you want to run execute that application using the java command, the syntax is the same as before, you just have to figure out which JAR files to include in your classpath.

I just created an example using Scala futures and found that I could execute/run the example using this command:

java -cp "/path/to/dotty-library_0.27-0.27.0-RC1.jar:/path/to/scala-library-2.13.3.jar:target/scala-0.27/multiplefutures2_0.27-0.1.0.jar" futures.MultipleFutures2

In that command, multiplefutures2_0.27-0.1.0.jar is the name of my JAR file, and futures.MultipleFutures2 is the name of my package and main method, respectively.

If you want to execute a JAR file built with Dotty, I hope this example is helpful

Here’s what the 10 a.m. sunrise looks like in Anchorage, Alaska on October 27th.

(While that picture is too dark to tell exactly where I was, I was at one of the two main hospitals in Anchorage that day for a medical procedure.)

10 a.m. sunrise, Anchorage, Alaska

Today I learned that when you’re using the MacOS Message application, after you type a first line or paragraph, you can start a new line or paragraph by typing [alt][Enter] instead of just [Enter]. If you type [Enter] you’ll send your message, but the [alt][Enter] keystroke combination lets you add new lines and create a new paragraph.

As a brief note, this example shows how to use the Scala 3 infix annotation — @infix. This solution also shows how to use the infix annotation with Scala 3 extension methods, because that’s what I need for my current situation:

import scala.annotation.infix

extension (i: Int)
   @infix def plus(j: Int) = i + j
   @infix def times(j: Int) = i + j

Given those infix + extension method definitions, this is how you use those infix methods:

1 plus 1    // 2
2 times 2   // 4

The REPL confirms this:

scala> 1 plus 1
val res0: Int = 2

scala> 2 times 2
val res1: Int = 4

Before I go, here’s a note from the Dotty / Scala 3 documentation related to infix methods:

A method annotation that suggests that the annotated method should be used as an infix operator. Infix operations with alphanumeric operator names require the operator to be annotated with @infix

In summary, if you wanted to see how to create a Scala 3 infix method, particularly as an extension method, I hope this example is helpful.

If I lay here
If I just lay here
Would you lie with me and just forget the world?

Today was a bit of a sad day, saying goodbye to some people I’ve known for the last nine years. So I’ve been listening to Chasing Cars, sung by Sarah Bettens.

“The true practice of meditation is to sit as if you are drinking water when you are thirsty.”

I used to meditate in what I now call a “lazy” way, more of a savasana “withdraw from the senses” style. These days my meditation is more active and alert, and I like this quote by Shunryu Suzuki:

“Treat every moment as your last.
It is not preparation for something else.”

(Last spring (2014), when I thought I might die as I laid in a bed in a hotel room, my meditation was extremely intense, just like this quote requests of us.)

I don’t know the original source of this image, but this quote is from the book, Zen Mind, Beginner’s Mind

Treat every moment as your last ~ Shunryu Suzuki

Here’s an example of how to use the Scala 3 @infix annotation to create an infix method, while using the technique with extension methods at the same time:

import scala.annotation.infix

extension (i: Int)
   @infix def plus(j: Int) = i + j
   @infix def times(j: Int) = i + j

That code adds the extension methods plus and times to the Scala Int class, while also letting them be used as infix methods. The REPL shows the result:

scala> 1 plus 1
val res0: Int = 2

scala> 2 times 2
val res1: Int = 4

In summary, if you wanted to see how to write infix methods and extension methods with Scala 3, I hope this example is helpful

This is a view of the Independence Mine area in Hatcher Pass, Alaska, from October, 2010.

View of Independence Mine, Hatcher Pass, Alaska

Hatcher Pass, Alaska, October 20, 2010.

Hatcher Pass, Alaska

This may look like a bad roller coaster ride, but it’s part of the Independence Mine in Hatcher Pass, Alaska. It operated as early as 1897, but was used more in the early-to-mid 1900s.

Independence Mine railroad in Hatcher Pass, Alaska