2
votes

I can't seem to overload "." and not sure if it's a compiler bug or something I'm doing:

@infix func . (a: Int, b: Int) -> Int {
  return a * b
}

I get the errors:

Expected identifier in function declaration Braced block of statements is an unused closure

2
You may use Extensions?Frank Fang

2 Answers

8
votes

You can't overload '.' It's a reserved token for the language. You can however overload the .. and ... operators.

Operators are made up of one or more of the following characters: /, =, -, +, !, , %, <, >, &, |, ^, ~, and .. That said, the tokens =, ->, //, /, */, ., and the unary prefix operator & are reserved. These tokens can’t be overloaded, nor can they be used to define custom operators.

Language Reference

3
votes

Swift does allow the definition and overload of custom operators, but it only allows certain characters to be considered operators.

Operators are made up of one or more of the following characters: /, =, -, +, !, *, %, <, >, &, |, ^, ~, and .. That said, the tokens =, ->, //, /*, */, ., and the unary prefix operator & are reserved. These tokens can’t be overloaded, nor can they be used to define custom operators.

It is therefore illegal to try to overload the period operator, though it can be used as part of another custom operator.