为什么没有快速存储类的存储类型属性? [英] Why no stored type properties for classes in swift?

查看:90
本文介绍了为什么没有快速存储类的存储类型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Swift编程语言,我惊讶地发现,与结构和枚举不同,类不支持存储的类型属性.

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

这是其他OO语言的共同特征,因此我认为有充分的理由他们决定不允许它.但是我无法猜测出这是什么原因,尤其是由于结构(和枚举)具有它们的原因.

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.

仅仅是因为它是Swift的早期版本,而尚未实现吗?还是在语言设计决策背后有更深层的原因?

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?

顺便说一句,存储类型属性"是Swift的术语.在其他语言中,这些可能称为类变量.示例代码:

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"

我现在意识到可以解决此限制,例如,通过使用具有存储属性的嵌套结构:

I now realize this limitation is trivial to work around, e.g., 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.

鉴于这一点以及Martin Gordon提到的编译器消息的措辞,我必须得出结论,这仅仅是一个(次要的)遗漏之处.

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.

推荐答案

编译器错误是尚不支持类变量",因此似乎它们尚未实现.

The compiler error is "Class variables not yet supported" so it seems like they just haven't implemented it yet.

这篇关于为什么没有快速存储类的存储类型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆