A Scala DSL example

I was just going through some old notes and found this Scala DSL example from 2010:

import scala.language.reflectiveCalls

object Dont extends App {

    def dont(codeBlock: => Unit) = new {
        def unless(condition: => Boolean) = if (condition) codeBlock
        def until(condition: => Boolean) = {
            while (!condition) {}
            codeBlock
        }
    }

    // (1) this will never be executed
    dont {
        println("If you see this, something is wrong.")
    }

    // (2) this will only be executed if the condition is true
    dont {
        println("Yes, 2 is greater than 1")
    } unless (2 > 1)

    // (3a) a little helper function for the `until` example that follows
    var number = 0
    def nextNumber = {
        number += 1
        println(number)
        number
    }

    // (3b) no output will be printed until the condition is met
    dont {
        println("Done counting to 5.")
    } until (nextNumber == 5)

}

I originally thought it was something I wrote, because the code at the beginning looks like variable names I would use, but after googling around it looks like it originally comes from this SO URL. Either way, it’s a decent little example of how to create a Scala DSL, so I thought I’d share it here (because it took quite a few searches before I found that SO link).