Naturally...

by Rob Poulter


The day after I go and look at writing an operator to mimic 'in' in Python, I read a blog post on undocumented Swift functions and come across the 'contains' function, which does pretty much that (although it doesn't work quite the same way for dicts and for strings it only works for character elements, so I guess I like one consistent operator better).

contains("happy", "a") // true
contains([1: "A", 2:"B"].keys,2) // true
contains([1,2,3,4,5,6],4) // true

As you can see the dictionary context of the function doesn't work without referring to the keys component of the dict. If you use a substring rather than a single character for the string context then you get an error since it doesn't conform to the generic used to define the function.