Calling a Swift Class Factory Method With Leading Dot Notation

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?

Why do I see some variables used with leading dot?

The method is (most likely) declared as

func autoPinEdge(_ from: UIRectEdge, toEdge: UIRectEdge, ofView: UIView)

so the compiler knows that the type of the first two parameters is UIRectEdge.


The full syntax to call the method is

view1.autoPinEdge(UIRectEdge.top, toEdge: UIRectEdge.bottom, ofView: view2)

but as the compiler knows (the documentation says can infer) the type you can pass only the members

view1.autoPinEdge(.top, toEdge: .bottom, ofView: view2)

Accessing a class variable directly (using dot notation) or via a function?

The class member temperature is not well designed. It lacks encapsulation.

  • The direct access changes the variable with little control. You have to know the internals of the class to be sure that the change doesn't lead to an inconsistent state. You cannot totally prevent the attempt to set an illegal value very easily.

  • The access via the function is designed deliver some promises, regardless of the internal details. Access via a function is more controlled: if the change of the temperature would require additional information (e.g. hygrometry because you expect to correlate in time both measurements, or timestamp from an external atomic clock) the function could required it, whereas variables can always be changed individually.

  • Moreover, if the thermometer will one day get some special behavior (e.g. update the display in the user's preferred unit of measure, or raise an alarm under some conditions), the function could make sure it is done.

So the access via the function is the safer approach, since it will protect you against future evolution of that class.

This being said, the design of the class could be improved:

  • The encapsulation should be enforced, making the variable private. The class users would then be forced to the proper use of registerTemperature() .
  • For convenience, you could make a setter on a wrapper property: this seems to give direct public access to a variable but when its value is changed the setter is executed, to ensure the promises of such changes are held.

Edit to avoid ambiguous wording: property observers (willSet and didSet) allow to produce additional effects when a public variable is changed directly. So direct changes do not prevent additional effects. But this require the class designer to have such direct changes in mind. And the change is less controlled than in a function. For example, you cannot totally prevent the effects of an illegal change: you could reverse a wrong change in didSet, but willSet would then be called twice; once with the illegal value, one after the reversing.

Omitting class name when using a class var from the same class

Swift can infer the type from the context. In that statement the compiler knows that self.view.backgroundColor is a UIColor. When you call the class method or property .white it automatically calls UIColor.white.

The convention is to use the second usage. It's shorter and descriptive enough for pretty much everyone to understand.

What is the Swift syntax .bar called?

It is called an implicit member expression. From the grammar section of the language guide:

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

.member name

For example:

var x = MyEnumeration.someValue
x = .anotherValue

Implicit Member Expression in Swift

This was a bug and was fixed at least in the latest Swift version 2.1

dot notation with p for hexadecimal numeric literals in swift

This is Hexadecimal exponential notation.

By convention, the letter P (or p, for "power") represents times two
raised to the power of
... The number after the P is decimal and
represents the binary exponent.

...

Example: 1.3DEp42 represents hex(1.3DE) × dec(2^42).

For your example, we get:

0xC.3p0 represents 0xC.3 * 2^0 = 0xC.3 * 1 = hex(C.3) = 12.1875

where hex(C.3) = dec(12.{3/16}) = dec(12.1875)

As an example, you can try 0xC.3p1 (equals hex(C.3) * dec(2^1)), which yields double the value, i.e., 24.375.

You can also study the binary exponent growth in a playground for hex-value 1:

// ...
print(0x1p-3) // 1/8 (0.125)
print(0x1p-2) // 1/4 (0.25)
print(0x1p-1) // 1/2 (0.5)
print(0x1p1) // 2.0
print(0x1p2) // 4.0
print(0x1p3) // 8.0
// ...

Finally, this is also explained in Apple`s Language Reference - Lexical Types: Floating-Point Literals:

Hexadecimal floating-point literals consist of a 0x prefix, followed
by an optional hexadecimal fraction, followed by a hexadecimal
exponent
. The hexadecimal fraction consists of a decimal point
followed by a sequence of hexadecimal digits. The exponent consists
of an upper- or lowercase p prefix followed by a sequence of decimal
digits that indicates what power of 2 the value preceding the p is
multiplied by
. For example, 0xFp2 represents 15 x 2^2, which
evaluates to 60. Similarly, 0xFp-2 represents 15 x 2^-2, which
evaluates to 3.75.



Related Topics



Leave a reply



Submit