Swift: my love for postfix printing

You may have noticed that, to date, Swift playgrounds are not the most stable. (You may add jokes here about the San Andreas Fault and your ex-significant-others.)  I often find myself copying items to command-line projects for testing.

Playgrounds offer the built-in results gutter to help determine when classes, structs, and functions are built and testing properly. With a command-line app, print statements replace single item evaluations. I prefer to do so in a way that doesn’t require tedious edits. Enter the postfix print operator.

operator postfix *** {}
@postfix func *** <T:Printable>(object : T) -> T {
    println(object.description)
    return object
}

What this operator gets me is something that I can easily use to convert items to print statements without editing to both the left and right. I can replace

myValue

with

myValue***

It also enables me to use a global find and replace to comment out the prints, replacing them with a sandbox-style declaration.

myValue //***

Nearly everyone I’ve shown this to reacts with noticeable horror, but I rather like its utility.

2 Comments

  • That *does* cause a double take. But, there’s not much assumption as to what a *** operator does. But, //*** is pretty close to some of the autodoc software tags.

  • Uh, I shiver with horror. Mostly because you replaced a completely comprehensible function (“println”) with something as cryptic and incomprehensible as it might possibly get. I don’t ask if YOU will still understand this five years from now, but it is going to be a pain in the backside for the next guy who will have to make sense of your code.

    I’ll put that down into my book of bad practices, if you don’t mind 😉