Protocols, `mutating` and Value vs Reference Types.


(Hooman Mehr) #1

Hi,

I am designing APIs that need to support both reference (class/object) and value types. I am running into restrictions of `mutating` keyword in my protocols and this is causing a lot of duplication of code. In order to understand what I mean please take a look at this gist <http://gist.github.com/hooman/2b74d9756976a43330c1acfcfba699f3>.

As you see, I have pairs of almost identical declarations: KeyValueStore vs KeyValueStoreObject, and AnyDictionaryStore vs AnyDictionaryStoreObject. This keeps rapidly growing as I am designing my APIs. Is there any sane way around this?

Thank you for your time,
Hooman


Mutating Function on Class Type (Possible bug?)
(Dave Abrahams) #2

protocol KeyValueStoreObject : class, KeyValueStore {}
protocol AnyDictionaryStoreObject : class, AnyDictionaryStore {}

would probably work for you.

HTH,

···

on Fri Apr 29 2016, Hooman Mehr <swift-users-AT-swift.org> wrote:

Hi,

I am designing APIs that need to support both reference (class/object) and value
types. I am running into restrictions of `mutating` keyword in my protocols and
this is causing a lot of duplication of code. In order to understand what I mean
please take a look at this gist.

As you see, I have pairs of almost identical declarations: KeyValueStore vs
KeyValueStoreObject, and AnyDictionaryStore vs AnyDictionaryStoreObject. This
keeps rapidly growing as I am designing my APIs. Is there any sane way around
this?

--
Dave


(Hooman Mehr) #3

Thank you very much for your response. I made the change in the gist <https://gist.github.com/hooman/2b74d9756976a43330c1acfcfba699f3>. This takes me back to the same error that forced my into duplication path:

“Cannot assign through subscript: ‘self’ is immutable” for the object case.

The gist <https://gist.github.com/hooman/2b74d9756976a43330c1acfcfba699f3> is self contained if you want to compile it yourself.

Is this some kind of bug or the expected behavior?

···

On Apr 29, 2016, at 5:28 PM, Dave Abrahams via swift-users <swift-users@swift.org> wrote:

on Fri Apr 29 2016, Hooman Mehr <swift-users-AT-swift.org> wrote:

Hi,

I am designing APIs that need to support both reference (class/object) and value
types. I am running into restrictions of `mutating` keyword in my protocols and
this is causing a lot of duplication of code. In order to understand what I mean
please take a look at this gist.

As you see, I have pairs of almost identical declarations: KeyValueStore vs
KeyValueStoreObject, and AnyDictionaryStore vs AnyDictionaryStoreObject. This
keeps rapidly growing as I am designing my APIs. Is there any sane way around
this?

protocol KeyValueStoreObject : class, KeyValueStore {}
protocol AnyDictionaryStoreObject : class, AnyDictionaryStore {}

would probably work for you.

HTH,

--
Dave

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


(Dave Abrahams) #4

Thank you very much for your response. I made the change in the gist. This takes
me back to the same error that forced my into duplication path:

“Cannot assign through subscript: ‘self’ is immutable” for the object case.

The gist is self contained if you want to compile it yourself.

Is this some kind of bug or the expected behavior?

Looks like a bug to me!

···

on Fri Apr 29 2016, Hooman Mehr <hooman-AT-mac.com> wrote:

    On Apr 29, 2016, at 5:28 PM, Dave Abrahams via swift-users > <swift-users@swift.org> wrote:

    on Fri Apr 29 2016, Hooman Mehr <swift-users-AT-swift.org> wrote:

        Hi,

        I am designing APIs that need to support both reference (class/object)
        and value
        types. I am running into restrictions of `mutating` keyword in my
        protocols and
        this is causing a lot of duplication of code. In order to understand
        what I mean
        please take a look at this gist.

        As you see, I have pairs of almost identical declarations: KeyValueStore
        vs
        KeyValueStoreObject, and AnyDictionaryStore vs AnyDictionaryStoreObject.
        This
        keeps rapidly growing as I am designing my APIs. Is there any sane way
        around
        this?

    protocol KeyValueStoreObject : class, KeyValueStore {}
    protocol AnyDictionaryStoreObject : class, AnyDictionaryStore {}

    would probably work for you.

    HTH,

    --
    Dave

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

--
Dave