[swift-users] Refining generics in classes

2017-11-15 Thread Jon Shier via swift-users
Swift Users:
I have a generics use case which has somewhat stumped me. I have two 
related protocols, JSONDecodable and CompressedDecodable, and 
CompressedDecodable inherits from JSONDecodable (though that relationship isn’t 
strictly necessary). I also have a generic function that’s overloaded for each 
of those protocols. I’m trying to write a class to make a network request 
expecting a generic response type of either JSONDecodable or 
CompressedDecodable. However, it doesn’t seem possible to write it in such a 
way that the overload I need is called. Instead, it’s always the superclass’ 
type’s overload that is called. For example:

protocol JSONDecodable { init() }
protocol CompressedDecodable: JSONDecodable { }

class NetworkRequest {

var response: T?

func doAThing() {
response = doSomething()
}
}

class CompressedNetworkRequest: NetworkRequest {

}

func doSomething() -> T {
print("One: \(T.self)")
return T()
}

func doSomething() -> T {
print("Two: \(T.self)")
return T()
}

struct Uno: JSONDecodable { }
struct Dos: CompressedDecodable { }

NetworkRequest().doAThing()
CompressedNetworkRequest().doAThing()

In a playground this prints:

One: Uno
One: Dos

Ultimately, I understand why this happens (NetworkRequest’s generic type is 
always going to be JSONDecodable, no matter if it’s actually a subtype). Is 
there any way, aside from completely duplicating the class, to call the 
overload appropriate for the type passed in a class like this? 



Jon Shier
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Swift Package Manager and iOS

2017-11-15 Thread Седых Александр via swift-users
Hello.
I start learn Swift Package Manager. 
Is exist a simple way to generate Xcode project with file Package.swift (which 
download all code) for iOS template in Xcode?
Something about 
$ swift package init --ios template =))


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


[swift-users] My mail for posts

2017-11-15 Thread mangal pavan via swift-users
Regards
mangal pavan
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Making a copy of an UnsafePointer

2017-11-15 Thread Rick Mann via swift-users


> On Nov 15, 2017, at 00:48 , Quinn The Eskimo! via swift-users 
>  wrote:
> 
> 
> On 15 Nov 2017, at 04:16, Rick Mann via swift-users  
> wrote:
> 
>> Is UnsafeMutablePointer<> not memory managed?
> 
> It is not.  Specifically, the code you posted creates a copy of the data and 
> then never destroys it.

Hmm. So it should leak, not crash. I wonder what I'm doing wrong.

> If I were in your shoes I’d construct a `Data` value from the unsafe pointer 
> and then pass that around.
> 
> let source = Data(bytes: inSourceBuffer, count: inSourceBufferLength)
> self.queue.async {
>let size = source.count
>source.withUnsafeBytes { (p: UnsafePointer) in
>self.foo(data: p, length: size)
>}
> }
> 
> The main drawback to this is that you have to jump through the hoops to 
> access the data unsafely.  It might be easier to recast your consumer (the 
> `foo(…)` method) in terms of `Data`.  That’s what I generally do when I work 
> with foreign APIs like this, that is, keep the data in ‘Swift space’ and only 
> deal with foreign types at the boundaries.
> 
> Whether that makes sense here really depends on the specifics of your 
> program.  For example, if your program has lots of this boundary code, it 
> might be nicer to just stick with the foreign type.  Or build a specific 
> wrapper that makes it easier to do this conversion.

Thanks. I have to call this code from Objective-C, passing a pointer and length 
(which is how I get the raw data). I could create CGImages from that, and I 
might, since eventually the consumer will be implemented in Metal 2. But right 
now, I just have to get back to the raw data to do the processing, so it seemed 
like extra work to create a CGImage.


-- 
Rick Mann
rm...@latencyzero.com


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


Re: [swift-users] Making a copy of an UnsafePointer

2017-11-15 Thread Quinn "The Eskimo!" via swift-users

On 15 Nov 2017, at 04:16, Rick Mann via swift-users  
wrote:

> Is UnsafeMutablePointer<> not memory managed?

It is not.  Specifically, the code you posted creates a copy of the data and 
then never destroys it.

If I were in your shoes I’d construct a `Data` value from the unsafe pointer 
and then pass that around.

let source = Data(bytes: inSourceBuffer, count: inSourceBufferLength)
self.queue.async {
let size = source.count
source.withUnsafeBytes { (p: UnsafePointer) in
self.foo(data: p, length: size)
}
}

The main drawback to this is that you have to jump through the hoops to access 
the data unsafely.  It might be easier to recast your consumer (the `foo(…)` 
method) in terms of `Data`.  That’s what I generally do when I work with 
foreign APIs like this, that is, keep the data in ‘Swift space’ and only deal 
with foreign types at the boundaries.

Whether that makes sense here really depends on the specifics of your program.  
For example, if your program has lots of this boundary code, it might be nicer 
to just stick with the foreign type.  Or build a specific wrapper that makes it 
easier to do this conversion.

Share and Enjoy
--
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


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