Swift TIL 11
Posted on 2020-07-11 21:34 +0100 in TIL • 2 min read
This is one of those things, especially this little play to help appreciate the feature, that I'm filing under "kinda cool, but I am never doing this in production".
So Swift has operator overriding, and then some. Moreover, operators are, in effect, functions, just with some extra syntax support. None of this is new to me, I've worked in and with other languages that have this ability, expect... I don't ever recall working in a language that, at the time I did, supported creating brand new operators (okay, fine, Lisp is a bit of an outlier here and there's all sorts of fun conversations to be had there -- but still, let's stick with more "conventional" syntax here). Support always seemed to be about extending the ability of an existing operator.
Swift though... yeah, you get to pick from a huge character space when it comes to creating operators.
Which got me thinking... How cool would it be to have a prefix operator that turns a floating point number into a currency-friendly number (you know, the sort of number type that can be used for currency-friendly calculations).
Swift has the
decimal
type which, at first glance anyway, looks to be a sensible candidate; even
if it isn't (and, really, how to actually sensibly handle currency is a
whole series of blog posts in their own right, that I have no wish to write
myself because such things are a previous working life for me, and other
people have doubtless done a very comprehensive job elsewhere over the
years) it will serve as a good stand-in for the little bit of horror I'm
going to write next.
So... Let's say we want to use £
as a prefix operator to say "see this
number? make it a decimal
", we could do something as simple as this:
import Foundation
prefix operator £
prefix func £( n : Decimal ) -> Decimal { n }
print( £1.20 + £0.75 + £0.01 )
Horrifically and delightfully, it works:
$ swift run
[3/3] Linking opover
1.96
I know, I know, I feel bad for even trying. But it's also kinda cool that the language has this.
It gets better though...
While reading up on what characters can and can't be used as operators, one thing that stood out was the fact that, more or less, any character that isn't a valid identifier can be used as an operator. So... hang on, we can use "emoji" as identifiers?
Like this?
import Foundation
prefix operator £
prefix func £( n : Decimal ) -> Decimal { n }
let 💵 = £1.20 + £0.75 + £0.01
print( 💵 )
Why yes. Yes we can. 😈