title: Swift - Create post- and prefix operator?
body: <p>In math it is common to write the amount of a number x as |x|. I would like to adopt a similar method to my code. My try on this looks like this:</p>
  
  <pre><code>prefix operator |
  postfix operator |
  
  extension Int {
      lazy var absolute = false
      static prefix func | (right: Int) -&gt; Int {
          assert(right.absolute, "Missed closing absolute value bar.")
          right.absolute = false
          if right &lt; 0 {
              return -value
          }
          return value
      }
      static postfix func | (left: Int) -&gt; Int {
          assert(!left.absolute, "Missed opening absolute value bar.")
          left.absolute = true
          return left
      }
  }
  </code></pre>
  
  <p>(I think this code won't compile as you cannot add stored properties in extensions as far as I know. It is only there to demonstrate my attempt. I added this functionality to my custom types.)</p>
  
  <p>Despite the fact that this feels like a rather bad solution to me, another problem with this code is, that it won't throw any error, if I forget the opening bar. The assert will only break the running program whenever I call another amount function after forgetting the opening bar in the previous amount function call.</p>
  
  <p>Let me know if you have a better solution! 
  Thanks.</p>