Custom Swift Prefix Operator for background and foreground operations

Few days ago I was writing some demo code to run a long process while showing the activity indicator. The code ended up looking like the following: 

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
            self.somethingTimeConsuming()
            
            // update the UI 
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.activityIndicator.stopAnimating()
            })
        }

At this point if I want to do anything on the background again I have to write the similar code again. I thought what would happen if I create the prefix operator for background and foreground processes. Here is the implementation:

import UIKit

typealias EmptyClosure = () -> ()

prefix operator ~ {}
prefix operator ~~ {}

prefix func ~~ (closure: EmptyClosure) {
    
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        closure() 
    }
}

prefix func ~ (closure: EmptyClosure) {
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
        closure()
    }
}

Now, I can rewrite my code like this: 

 ~{ // run on the background thread
            self.somethingTimeConsuming()
            
            ~~{ // run on the main thread
                self.activityIndicator.stopAnimating()
              }
            
        }

Much shorter but also not crystal clear!

The point of this post is not that you should start creating your operators but to show that you *can* create your operators and use them in meaningful scenarios.