简体   繁体   中英

Why no stored type properties for classes in swift?

Working through The Swift Programming Language, I was surprised to see that, unlike structures and enumerations, classes do not support stored type properties.

This is a common feature of other OO languages so I assume there was a good reason they decided not to allow it. But I'm not able to guess what that reason is, especially since structures (and enumerations) have them.

Is it simply that it's early times for Swift and it just hasn't been implemented yet? Or is there a deeper reason behind language design decision?

BTW, "stored type property" is Swift terminology. In other languages these might be called class variables. Example code:

struct FooStruct {
    static var storedTypeProp = "struct stored property is OK"
}

FooStruct.storedTypeProp // evaluates to "struct stored property is OK"

class FooClass {
    class var computedClassProp: String { return "computed class property is OK" }

    // class var storedClassProp = "class property not OK" // this won't compile
}

FooClass.computedClassProp // evaluates to "computed class property is OK"

Edit:

I now realize this limitation is trivial to work around, eg, by using a nested structure with stored properties:

class Foo {
    struct Stored {
        static var prop1 = "a stored prop"
    }
}

Foo.Stored.prop1 // evaluates to "a stored prop"
Foo.Stored.prop1 = "new value"
Foo.Stored.prop1 // evaluates to "new value"

That seems to preclude their being some deep inscrutable language design reason for this limitation.

Given that and the wording of the compiler message that Martin Gordon mentions, I have to conclude that this is simply something (minor) left out.

编译器错误是“尚未支持的类变量”,所以看起来他们还没有实现它。

Extending the OP's nested struct trick for simulating stored type properties , you can go further and make it look like a pure stored type property from outside the class.

Use a computed getter and setter pair like:

class ClassWithTypeProperty
{
    struct StoredTypeProperties
    {
        static var aTypeProperty: String = "hello world"
    }

    class var aTypeProperty: String
    {
        get { return self.StoredTypeProperties.aTypeProperty }
        set { self.StoredTypeProperties.aTypeProperty = newValue }
    }
}

Then you can do:

println(ClassWithTypeProperty.aTypeProperty)
// Prints "hello world"

ClassWithTypeProperty.aTypeProperty = "goodbye cruel world"

println(ClassWithTypeProperty.aTypeProperty)
// Prints "goodbye cruel world"

“For value types (that is, structures and enumerations), you can define stored and computed type properties. For classes, you can define computed type properties only."

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l

I think it's easy for Apple's Engineers to add stored type properties to classes, but not yet we know, maybe never in my opinion. And that's why there are labels ( static and class ) to distinguish them.

The most important reason may be it:

To avoid different objects have shared changeable variable

we know :

static let storedTypeProperty = "StringSample"  // in struct or enum ...

can be replaced by

class var storedTypeProperty:String {return "StringSample" }  // in class

but

static var storedTypeProperty = "StringSample"  

is harder to be replaced by class phrase in class.

// I am new to Swift Programming Language actually and it's my first answer in Stack OverFlow. Glad to discuss with you. ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM