null

Functional error handling in Scala

Because functional programming is like algebra, there are no null values or exceptions. But of course you can still have exceptions when you try to access servers that are down or files that are missing, so what can you do? This lesson demonstrates the techniques of functional error handling in Scala.

Examples of how to use 'let' in Kotlin

As a little note to self, here are a few let examples in Kotlin:

fun main(args: Array<String>) {

    fun toUpper(s: String?): String? = s?.toUpperCase()

    toUpper(null)?.let {
        // this will never be run
        println("toUpper(null) is: ${it}")
    }

    toUpper("emily")?.let {
        println("toUpper(emily) is: ${it}")
    }

    // `let` can return a value
    val hello = toUpper("Hannah")?.let {
        "Hello, ${it}"
    }
    println(hello)

}

The output looks like this:

How to test String equality in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.1, “Testing String Equality in Scala.”

Problem

When using Scala, you want to compare two strings to see if they’re equal, i.e., whether they contain the exact same sequence of characters.

Solution

In Scala you compare two String instances with the == operator. Given these strings:

This is a page from my book, Functional Programming, Simplified

Rules for Programming in This Book

“Learn the rules like a pro, so you can
break them like an artist.”

Pablo Picasso

“Learn the rules, and then forget them.”
Haiku Master Matsuo Basho

Alright, that’s enough of the “preface” material, let’s get on with the book!

As I wrote earlier, I want to spare you the route I took of, “You Have to Learn Haskell to Learn Scala/FP,” but, I need to say that I did learn a valuable lesson by taking that route:

Scala best practice: How to use the Option/Some/None pattern

Table of Contents1 - Problem2 - Solution3 - Returning an Option from a method4 - Getting the value from an Option5 - Using Option with Scala collections6 - Using Option with other frameworks7 - Using Try, Success, and Failure8 - Using Either, Left, and Right9 - Discussion10 - Don’t use the get method with Option11 - See Also12 - The Scala Cookbook

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.6, “Scala best practice: How to use the Option/Some/None pattern.”

Back to top

Problem

For a variety of reasons, including removing null values from your Scala code, you want to use what I call the Option/Some/None pattern. Or, if you’re interested in a problem (exception) that occurred while processing code, you may want to return Try/Success/Failure from a method instead of Option/Some/None.

How to cast a null value in Java alvin February 4, 2016 - 2:12pm

Until a little while ago I don’t think I had ever thought about intentionally casting a null value in Java, but then I ran into a problem and realized that the solution was to cast a null value, like this:

FileDialog d = new FileDialog((java.awt.Frame) null);

You have to do that in this case because FileDialog has several one-argument constructors, including one that takes a JFrame and another that takes a JDialog. If you just put null in the constructor the Java compiler or your favorite IDE will complain, so you have to cast the null value to one of those specific types, and this syntax shows how to do this. (My app uses multiple frames, and at the moment I’d rather put null in the FileDialog constructor than try to determine which frame is currently in the foreground.)