Calling a Swift class factory method with leading dot notation?

This feature is called “Implicit Member Expression

An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a class method, in a context where type inference can determine the implied type. It has the following form:

.member name


But, as of right now, I advise you should not use this feature in Optional or ImplicitlyUnwrappedOptional context.

Although this works:

// store in Optional variable
let col: UIColor?
col = .redColor()

// pass to function
func f(arg:UIColor?) { println(arg) }
f(.redColor())

This crashes the compiler 🙁

func f(arg:UIColor?, arg2:Int) { println(arg) }
//                 ^^^^^^^^^^ just added this.
f(.redColor(), 1)

The compiler has some bugs. see: does swift not allow initialization in function parameters?

Leave a Comment