Re: [swift-users] Weak references in generic types

2016-08-31 Thread Karl via swift-users
> On 1 Sep 2016, at 03:23, Howard Lovatt via swift-users > wrote: > > Playing around I found that if you make the protocol @objc instead of > AnyObject then it works :). EG: > > struct WeakReference { > weak var value: T? > } > @objc protocol P { // Note @objc, class or AnyObject does not

[swift-users] Subtract a set of a subclass?

2016-08-31 Thread Nick Brook via swift-users
I have a set, Set and a subset of that, Set, where B: A. I want to subtract Set from Set, but the subtract function signature specifies that the set elements must be the same type (S.Generator.Element == Element). I guess this is because Element is not required to be a class, simply hashable, t

Re: [swift-users] Subtract a set of a subclass?

2016-08-31 Thread Zhao Xin via swift-users
I don't see the point. For example if an element in Set and another element in Set are with a same hash value. Neither of the elements should be subtracted. As they are in different types. And hash values between different types are not guaranteed to be comparable. import Foundation class Foo:Ha

[swift-users] Subtract a set of a subclass?

2016-08-31 Thread Howard Lovatt via swift-users
I am probably misunderstanding you, but it seems to work for me: class A: Hashable, CustomStringConvertible { var hashValue: Int { return ObjectIdentifier(self).hashValue } var description: String { return "A@\(hashValue)" } } func ==(lhs: A, rhs: A) -> Bool { return lhs.hashValue == r

Re: [swift-users] Weak references in generic types

2016-08-31 Thread Howard Lovatt via swift-users
Playing around I found that if you make the protocol @objc instead of AnyObject then it works :). EG: struct WeakReference { weak var value: T? } @objc protocol P { // Note @objc, class or AnyObject does not work var i: Int { get } } class CP: P { var i: Int = 0 } let weakPs: [WeakRefe

[swift-users] Function to unsafe pointer and back

2016-08-31 Thread Lou Zell via swift-users
Hi all, In both examples below I'm attempting to call a function from the function's address. The first example works but the second doesn't. I suspect I'm missing something fundamental about function invocations. Can someone explain why the second example fails? First: func doNothing() {} fu

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-31 Thread Jens Alfke via swift-users
That’s going to be quite slow, like O(n^2), since it copies the dictionary every time it adds a key to it. It also seems more complicated than the simple for-loop implementation; but then, I’m not a functional-language programmer. // here’s how I’d do it; not tested in a playground var dict2 = [

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-31 Thread Daniel Tartaglia via swift-users
Sorry, I was too quick to send that email… Here is something from a playground that works: extension Dictionary { /// An immutable version of update. Returns a new dictionary containing self's values and the key/value passed in. func withUpdate(key: Key, value: Value) ->

Re: [swift-users] How to extend a generic type with a Bool constraint?

2016-08-31 Thread Jens Alfke via swift-users
> On Aug 31, 2016, at 11:09 AM, Jens Alfke wrote: > > I’m also bemused that (as I said) there is a boolean protocol in Swift 2.2, > and a different one in master (according to swiftdoc.org > ), but none in Swift 3.0 (as of Xcode 8 beta 6). Is > this just a glitch that sn

Re: [swift-users] How to extend a generic type with a Bool constraint?

2016-08-31 Thread Jens Alfke via swift-users
> On Aug 31, 2016, at 10:21 AM, Dan Loewenherz wrote: > > You'll need to specify a protocol where you're currently specifying "Bool". A > generic constraint can't take a concrete type as an argument. OK … this should be described in the book, though. (Any doc writers listening? Or is there a

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-31 Thread Jens Alfke via swift-users
> On Aug 31, 2016, at 10:51 AM, Daniel Tartaglia wrote: > > I use the below to do this: > > dict2 = dict1.map { $0.withUpdate($0.0, value: $0.1) } I don’t see how this can work. Dictionary.map returns an Array, not a Dictionary. Also, in the callback function $0 is the key, so your Dictionary

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-31 Thread Daniel Tartaglia via swift-users
I use the below to do this: dict2 = dict1.map { $0.withUpdate($0.0, value: $0.1) } The above will effectively do a straight copy, you will probably want to modify the values passed in though… https://gist.github.com/dtartaglia/6141e6003e408399c2dd7a7cc266dce6#file-dictionaryextensions-swift ex

Re: [swift-users] How to extend a generic type with a Bool constraint?

2016-08-31 Thread Dan Loewenherz via swift-users
You'll need to specify a protocol where you're currently specifying "Bool". A generic constraint can't take a concrete type as an argument. So you could do this: protocol BooleanType {} extension Bool: BooleanType {} extension foo where T: BooleanType { ... } If you don't like this, you can d

Re: [swift-users] How to extend a generic type with a Bool constraint?

2016-08-31 Thread Quinn "The Eskimo!" via swift-users
`Boolean` (the protocol) was removed in Swift 3 per SE-0109 “Remove the Boolean protocol”. You can do what you want by defining your own dummy protocol and extending `Bool` (the type) to conform to that. p

Re: [swift-users] tuples and metatypes

2016-08-31 Thread Quinn "The Eskimo!" via swift-users
On 31 Aug 2016, at 02:38, Ray Fix via swift-users wrote: > Is it correct to say that this is true because there is not a unique metatype > for compound types? The way I’ve seen this explained is that tuples are "structural types" (their compatibility is determined by their structure) and oth

[swift-users] How to extend a generic type with a Bool constraint?

2016-08-31 Thread Jens Alfke via swift-users
I have a generic type, and I want to add some methods that are available when the type parameter is Bool. (This is in Xcode 8 beta 6, btw.) public struct foo { ... } extension foo where T: Bool { … } The above fails to compile, with "type 'T' constrained to non-protocol type ‘B

[swift-users] tuples and metatypes

2016-08-31 Thread Ray Fix via swift-users
Hi. Asking for a friend ;] Tuples cannot conform to protocols or be extended because they are compound types. Is it correct to say that this is true because there is not a unique metatype for compound types? IOW, there is no unique existential container thingy for a tuples? (Not sure if I am

Re: [swift-users] remove "Example" from multiline playground markup

2016-08-31 Thread Erica Sadun via swift-users
To the best of my knowledge, this is a rendering choice made by the playground team and you cannot suppress it. Workaround: Use `whatever` code voice instead of code fencing. Playground markup is currently out of step with commonmark 0.25, which allows you to use a subset of raw HTML including

Re: [swift-users] remove "Example" from multiline playground markup

2016-08-31 Thread Ray Fix via swift-users
Thank you for the info and the workaround Erica! I typically use the multiline code for things other than examples such as problem statements. I have filed radar 28088652 in hope that someone on the Xcode team agrees that there should be a better solution. Thank you, Ray Fix > On Aug 30, 201