[discussion] Class stored properties?

Curious if class stored properties have ever been discussed (doesn't seem
so..)?

Also, assuming no, and assuming there's a good reason no/they're not coming
anytime soon, what are some patterns people have been using in their place?
I was considering something like..

class SomeClass {
    private static var _classValues = [SomeClass.Type: Int]()

    class var value: Int {
        get { _classValues[self] }
        set { _classValues[self] = newValue }
    }
}

.. but then I remembered types aren't hashable yet either (so I guess I
need to stringify it first).

This is a singleton, it just happens to be in class scope.

~Robert Widmann

···

On Aug 9, 2017, at 3:55 AM, Mathew Huusko V via swift-evolution <swift-evolution@swift.org> wrote:

Curious if class stored properties have ever been discussed (doesn't seem so..)?

Also, assuming no, and assuming there's a good reason no/they're not coming anytime soon, what are some patterns people have been using in their place? I was considering something like..

class SomeClass {
    private static var _classValues = [SomeClass.Type: Int]()

    class var value: Int {
        get { _classValues[self] }
        set { _classValues[self] = newValue }
    }
}

.. but then I remembered types aren't hashable yet either (so I guess I need to stringify it first).
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Hi,

can you provide a usecase for this? I remember this being used in ObjC for +version, but I'm not sure anymore where I'd use this...

BTW you don't need to stringify, use ObjectIdentifier (part of stdlib).

···

On Aug 9, 2017, at 12:54 PM, Mathew Huusko V via swift-evolution <swift-evolution@swift.org> wrote:

Curious if class stored properties have ever been discussed (doesn't seem so..)?

Also, assuming no, and assuming there's a good reason no/they're not coming anytime soon, what are some patterns people have been using in their place? I was considering something like..

class SomeClass {
    private static var _classValues = [SomeClass.Type: Int]()

    class var value: Int {
        get { _classValues[self] }
        set { _classValues[self] = newValue }
    }
}

.. but then I remembered types aren't hashable yet either (so I guess I need to stringify it first).
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I don't follow. What's a singleton/how? If you mean my example, the whole
point/need for the feature is so it's not. I want `.value` to be unique
storage for each subclass.

···

On Wed, Aug 9, 2017 at 7:09 PM, Robert Widmann <rwidmann@apple.com> wrote:

This is a singleton, it just happens to be in class scope.

~Robert Widmann

> On Aug 9, 2017, at 3:55 AM, Mathew Huusko V via swift-evolution < > swift-evolution@swift.org> wrote:
>
> Curious if class stored properties have ever been discussed (doesn't
seem so..)?
>
> Also, assuming no, and assuming there's a good reason no/they're not
coming anytime soon, what are some patterns people have been using in their
place? I was considering something like..
>
> class SomeClass {
> private static var _classValues = [SomeClass.Type: Int]()
>
> class var value: Int {
> get { _classValues[self] }
> set { _classValues[self] = newValue }
> }
> }
>
> .. but then I remembered types aren't hashable yet either (so I guess I
need to stringify it first).
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

Class variables are not common, but they are the most natural way to implement class-specific functionality. Off the top of my head, I have used class variables for:

1) Custom allocation schemes, e.g. pools, LRU implementations
2) Diagnostics, e.g. counting the number of instances of a given class
3) Plugin-in functionality, e.g. customizing behavior of a class at runtime. In Swift, the variables would be closures.

You can argue that class variables these are not strictly necessary for these tasks, but I have found them to be handy, including during development and “Bring-up"

- Chris

···

On Aug 9, 2017, at 12:52 PM, Mathew Huusko V via swift-evolution <swift-evolution@swift.org> wrote:

I don't follow. What's a singleton/how? If you mean my example, the whole point/need for the feature is so it's not. I want `.value` to be unique storage for each subclass.

On Wed, Aug 9, 2017 at 7:09 PM, Robert Widmann <rwidmann@apple.com <mailto:rwidmann@apple.com>> wrote:
This is a singleton, it just happens to be in class scope.

~Robert Widmann

> On Aug 9, 2017, at 3:55 AM, Mathew Huusko V via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>
> Curious if class stored properties have ever been discussed (doesn't seem so..)?
>
> Also, assuming no, and assuming there's a good reason no/they're not coming anytime soon, what are some patterns people have been using in their place? I was considering something like..
>
> class SomeClass {
> private static var _classValues = [SomeClass.Type: Int]()
>
> class var value: Int {
> get { _classValues[self] }
> set { _classValues[self] = newValue }
> }
> }
>
> .. but then I remembered types aren't hashable yet either (so I guess I need to stringify it first).
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org <mailto:swift-evolution@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

It's common for a class to return a value (class var), but setting it on a per-class basis IMHO isn't *that* common. But maybe I'm mistaken.

···

On Aug 9, 2017, at 1:09 PM, Mathew Huusko V <mhuusko5@gmail.com> wrote:

My use case is an abstract class. In Objective-C I used associated values on the dynamic class object, and I had come to think this was relatively common. I guess not.

On Wed, Aug 9, 2017 at 12:01 PM, Charlie Monroe <charlie@charliemonroe.net <mailto:charlie@charliemonroe.net>> wrote:
Hi,

can you provide a usecase for this? I remember this being used in ObjC for +version, but I'm not sure anymore where I'd use this...

BTW you don't need to stringify, use ObjectIdentifier (part of stdlib).

> On Aug 9, 2017, at 12:54 PM, Mathew Huusko V via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>
> Curious if class stored properties have ever been discussed (doesn't seem so..)?
>
> Also, assuming no, and assuming there's a good reason no/they're not coming anytime soon, what are some patterns people have been using in their place? I was considering something like..
>
> class SomeClass {
> private static var _classValues = [SomeClass.Type: Int]()
>
> class var value: Int {
> get { _classValues[self] }
> set { _classValues[self] = newValue }
> }
> }
>
> .. but then I remembered types aren't hashable yet either (so I guess I need to stringify it first).
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org <mailto:swift-evolution@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution