Unsubscribe


(Howard Perlman) #1

Unsubscribe

···

From: "swift-users-request@swift.org" <swift-users-request@swift.org>
To: swift-users@swift.org
Sent: Friday, July 14, 2017 12:57 PM
Subject: swift-users Digest, Vol 20, Issue 13
   
Send swift-users mailing list submissions to
swift-users@swift.org

To subscribe or unsubscribe via the World Wide Web, visit
https://lists.swift.org/mailman/listinfo/swift-users
or, via email, send a message with subject or body 'help' to
swift-users-request@swift.org

You can reach the person managing the list at
swift-users-owner@swift.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of swift-users digest..."

Today's Topics:

1. Swift 4 emulating Decoder behaviour (Joanna Carter)

----------------------------------------------------------------------

Message: 1
Date: Thu, 13 Jul 2017 20:46:20 +0200
From: Joanna Carter <joanna@carterconsulting.org.uk>
To: swift-users@swift.org
Subject: [swift-users] Swift 4 emulating Decoder behaviour
Message-ID:
<A8DE64E2-5360-4B35-87D2-69F3F5B562E7@carterconsulting.org.uk>
Content-Type: text/plain; charset=utf-8

Greetings

I notice that, with Swift 4, I can decode an object like this :

• let retrievedSpot = try decoder\.decode\(ParkingSpot\.self, from: retrievedData\)  

And that will return a ParkingSpot, as expected, into retrievedSpot.

However, I thought I would try the same technique with one of my pet projects…

I have a protocol and an implementing struct :

• public protocol PropertyProtocol  
• \{  
•  static var propertyType: Any\.Type \{ get \}  
•  
•  var untypedValue: Any? \{ get \}  
• \}  
•  
• public struct Property&lt;valueT : DefaultValueProvider&gt; : PropertyProtocol  
• \{  
•  public static var propertyType: Any\.Type  
•  \{  
•    return valueT\.self  
•  \}  
•  
•  public var untypedValue: Any?  
•  \{  
•    return value  
•  \}  
•  
•  public var value = valueT\(\)  
• \}  

Now, I want to be able to use a "factory" method to create an instance of Property<T>, bound to its parameter type. So, I followed the same principal as Decoder :

• struct PropertyFactory  
• \{  
•  static func createProperty&lt;T&gt;\(\_ type: T\.Type\) \-&gt; PropertyProtocol where T : DefaultValueProvider  
•  \{  
•    return type\.createProperty\(\)  
•  \}  
• \}  

DefaultValueProvider is defined as follows and String is extended to conform to it :

• public protocol DefaultValueProvider  
• \{  
•  init\(\)  
• \}  
•  
• extension String : DefaultValueProvider \{ \}  

Now, this works fine if I pass a known type :

• let pproperty = PropertyFactory\.createProperty\(String\.self\)  

But, if I hold the type to be passed in in an Any.Type or DefaultValueProvider.Type variable, then doing this :

•    let type: Any\.Type = String\.self  
•  
•    if let propertyType = type as? DefaultValueProvider\.Type  
•    \{  
•      let p = PropertyFactory\.createProperty\(propertyType\)  
•      
•      …  

Fails to compile with the message : Cannot invoke 'createProperty' with an argument list of type '(DefaultValueProvider.Type)'

Likewise Decoder.decode(…) will not accept storing the type in an Any.Type or Decodable.Type variable.

I find this odd and perplexing. Is this a known issue or has nobody yet realised that this could be useful ?

Joanna

--
Joanna Carter
Carter Consulting

------------------------------

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

End of swift-users Digest, Vol 20, Issue 13
*******************************************


(Jordan Rose) #2

Oops, thought the mailing list software was supposed to catch this. Unsubscribed you, Howard; sorry for the spurious message, everyone else.

-Jordan

···

On Jul 14, 2017, at 10:03, Howard Perlman via swift-users <swift-users@swift.org> wrote:

Unsubscribe

From: "swift-users-request@swift.org" <swift-users-request@swift.org>
To: swift-users@swift.org
Sent: Friday, July 14, 2017 12:57 PM
Subject: swift-users Digest, Vol 20, Issue 13

Send swift-users mailing list submissions to
    swift-users@swift.org <mailto:swift-users@swift.org>

To subscribe or unsubscribe via the World Wide Web, visit
    https://lists.swift.org/mailman/listinfo/swift-users
or, via email, send a message with subject or body 'help' to
    swift-users-request@swift.org <mailto:swift-users-request@swift.org>

You can reach the person managing the list at
    swift-users-owner@swift.org <mailto:swift-users-owner@swift.org>

When replying, please edit your Subject line so it is more specific
than "Re: Contents of swift-users digest..."

Today's Topics:

  1. Swift 4 emulating Decoder behaviour (Joanna Carter)

----------------------------------------------------------------------

Message: 1
Date: Thu, 13 Jul 2017 20:46:20 +0200
From: Joanna Carter <joanna@carterconsulting.org.uk <mailto:joanna@carterconsulting.org.uk>>
To: swift-users@swift.org <mailto:swift-users@swift.org>
Subject: [swift-users] Swift 4 emulating Decoder behaviour
Message-ID:
    <A8DE64E2-5360-4B35-87D2-69F3F5B562E7@carterconsulting.org.uk <mailto:A8DE64E2-5360-4B35-87D2-69F3F5B562E7@carterconsulting.org.uk>>
Content-Type: text/plain; charset=utf-8

Greetings

I notice that, with Swift 4, I can decode an object like this :

    • let retrievedSpot = try decoder.decode(ParkingSpot.self, from: retrievedData)

And that will return a ParkingSpot, as expected, into retrievedSpot.

However, I thought I would try the same technique with one of my pet projects…

I have a protocol and an implementing struct :

    • public protocol PropertyProtocol
    • {
    • static var propertyType: Any.Type { get }
    •
    • var untypedValue: Any? { get }
    • }
    •
    • public struct Property<valueT : DefaultValueProvider> : PropertyProtocol
    • {
    • public static var propertyType: Any.Type
    • {
    • return valueT.self
    • }
    •
    • public var untypedValue: Any?
    • {
    • return value
    • }
    •
    • public var value = valueT()
    • }

Now, I want to be able to use a "factory" method to create an instance of Property<T>, bound to its parameter type. So, I followed the same principal as Decoder :

    • struct PropertyFactory
    • {
    • static func createProperty<T>(_ type: T.Type) -> PropertyProtocol where T : DefaultValueProvider
    • {
    • return type.createProperty()
    • }
    • }

DefaultValueProvider is defined as follows and String is extended to conform to it :

    • public protocol DefaultValueProvider
    • {
    • init()
    • }
    •
    • extension String : DefaultValueProvider { }

Now, this works fine if I pass a known type :

    • let pproperty = PropertyFactory.createProperty(String.self)

But, if I hold the type to be passed in in an Any.Type or DefaultValueProvider.Type variable, then doing this :

    • let type: Any.Type = String.self
    •
    • if let propertyType = type as? DefaultValueProvider.Type
    • {
    • let p = PropertyFactory.createProperty(propertyType)
    •
    • …

Fails to compile with the message : Cannot invoke 'createProperty' with an argument list of type '(DefaultValueProvider.Type)'

Likewise Decoder.decode(…) will not accept storing the type in an Any.Type or Decodable.Type variable.

I find this odd and perplexing. Is this a known issue or has nobody yet realised that this could be useful ?

Joanna

--
Joanna Carter
Carter Consulting

------------------------------

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

End of swift-users Digest, Vol 20, Issue 13
*******************************************

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