Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowed method / function names in Swift

This is a two-parts question and I couldn't find an answer inside 'The Swift Programming Language' so far.

The first question is:
What characters are allowed as names of method or functions in Swift?
Are all unicode characters allowed?

The second part of the question is:
Are there some exceptions like reserved names or characters?
Is there an official list of them somewhere?

The reason why I'm asking is that I tried to write a function like this and received an error:

func ?(object: AnyObject) -> AnyObject {
    // some function
    return object
}

But when I try using other non-latin characters like ü, ä, ö it works, the following doesn't throw an error:

func fünctiön(object: AnyObject) -> AnyObject {
    // some function
    return object
}

So, this is confusing me.

like image 440
Jeehut Avatar asked Jul 23 '14 11:07

Jeehut


1 Answers

From the reference book:

Identifiers begin with an uppercase or lowercase letter A through Z, an underscore (_), a noncombining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area. After the first character, digits and combining Unicode characters are also allowed.

The Lexical Structure section actually lists all the Unicode Ranges allowed for the first character and the other characters.

The reason is obvious - the compiler has to distinguish between identifiers and operators. Allowing punctuation characters in identifiers would make it impossible to differ between an identifier and an operator.

like image 151
Sulthan Avatar answered Oct 30 '22 02:10

Sulthan