There are many places that () can be useful when playing with "CS" issues, which often take the form "implement X using Y, even if you really have X." So, for example, I can say implement Set using Dictionary. Well, a dictionary is a key / value pair. What should be the type of value? I actually saw this in languages with dictionaries, but not with sets, and people often use 1 or true as their value. But this is not exactly what you mean. This opens up ambiguity. What if the value is false? Is it bundled or not? The correct way to implement Set in terms of a dictionary is [Key: ()] , and then you end the lines of code, for example:
set[key] = ()
There are other equivalent versions, for example your Optional<()> . I could also implement integers like [()] or Set<()> . It's a little stupid, but I did such things to research number theory earlier.
However, these are all almost intentionally impractical decisions. How about practical? Usually they appear when developing common programs. For example, imagine a function with this form:
func doThingAndReturn<T>(retval: T, f: () -> Void) -> T { f() return retval }
This is not as stupid as it seems. Something along these lines can easily be displayed in the Command pattern. But what if there is no retval; I don't care about returning? Well, that’s fine, just pass the value () .
func doThing(f: () -> Void) { doThingAndReturn((), f: f) }
Similarly, you may need a function like zipMap :
func zipMap<T, U>(funcs: [(T) -> U], vals: [T]) -> [U] { return zip(funcs, vals).map { $0($1) } }
This applies a series of functions that take T to values of type T We could use this even if T happens to () , but we need to create a bunch of () values for this to work. For instance:
func gen<T>(funcs: [() -> T]) -> [T] { return zipMap(funcs, vals: Array(count: funcs.count, repeatedValue: ())) }
I would not expect this to happen very often in Swift, because Swift is basically an imperative language and hides its Void almost all cases. But you really see things that appear in functional languages like Scala when they go into imperative programming.