[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, 
therefore inheritance is not guaranteed? Is there any way this could be 
implemented in Set, in an extension, or what would be the most efficient way to 
perform that operation?

Thanks

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


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:Hashable {

var value: Int



public var hashValue: Int {

return value

}



public static func ==(lhs: Foo, rhs: Foo) -> Bool {

return lhs.value == rhs.value

}



required init(_ value:Int) {

self.value = value

}

}


class Bar:Foo {

override public var hashValue: Int {

return value * 10

}

}


let foo = Foo(10)

let bar = Bar(10)


print(foo.hashValue) // 10

print(bar.hashValue) // 100

print((bar as Foo).hashValue) // 100 instead of 10


print(foo == bar) // true

print(foo.hashValue == bar.hashValue) // false

As you can see in the above code, although `foo == bar` is true,
`foo.hashValue == bar.hashValue` is not guaranteed to be true. As far as I
know, Set uses hash values instead of equations to compare the elements.
So the results of a super class and its sub class are not guaranteed. Also,
as `(bar as Foo).hashValue` is always the result of its own class, you
can't get the results you want through casting.

var fooSet:Set = [Foo(10), Foo(9), Foo(8), Foo(7)]

var barSet:Set = [Bar(8), Bar(7), Bar(6), Bar(5)]


fooSet.subtract(barSet)

fooSet.forEach { print("\(type(of:$0)), value:\($0.value)") }

/*

 Foo, value:10

 Foo, value:9

 Foo, value:8 // Here is a mystery, Foo(7) is unreasonably missing.

*/

However, if you can guarantee the hash values are comparable, you still can
get the results you want.

class Foo:Hashable {

var value: Int



public var hashValue: Int {

return value

}



public static func ==(lhs: Foo, rhs: Foo) -> Bool {

return lhs.value == rhs.value

}



required init(_ value:Int) {

self.value = value

}

}


class Bar:Foo {

var name = "bar"

}


var fooSet:Set = [Foo(10), Foo(9), Foo(8), Foo(7)]

var barSet:Set = [Bar(8), Bar(7), Bar(6), Bar(5)]


fooSet.subtract(barSet)

fooSet.forEach { print("\(type(of:$0)), value:\($0.value)") }

/*

 Foo, value:10

 Foo, value:9

*/


Zhaoxin

On Thu, Sep 1, 2016 at 8:31 AM, Nick Brook via swift-users <
swift-users@swift.org> wrote:

> 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, therefore inheritance is not guaranteed? Is there any way
> this could be implemented in Set, in an extension, or what would be the
> most efficient way to perform that operation?
>
> Thanks
>
> Nick
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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: [WeakReference] = [WeakReference(value: cP)] // Note typed
as `[WeakReference]`
print("P: \(weakPs[0].value!.i)") // 0

Not a 'pure' Swift solution :(, but OK in my case.

  -- Howard.

On 29 August 2016 at 16:21, Howard Lovatt  wrote:

> Hi,
>
> I am wanting to use weak references in generic data structures; in the
> example below Array, but in general any generic type. I can almost get it
> to work :(
>
> My experiments started off well; the following works:
>
> // Array of weak references OK
> struct WeakReference {
> weak var value: T?
> }
> class C {
> var i: Int = 0
> }
> let c = C() // Strong reference to prevent collection
> let weakCs = [WeakReference(value: c)] // OK
> print("C: \(weakCs[0].value!.i)") // 0
>
>
> I can add a protocol:
>
> // Array of weak references that implements a protocol OK
> protocol P: AnyObject { // Note AnyObject
> var i: Int { get }
> }
> class CP: P {
> var i: Int = 0
> }
> let cP = CP() // Strong reference to prevent collection
> let weakCPs = [WeakReference(value: cP)] // OK
> print("CP: \(weakCPs[0].value!.i)") // 0
>
>
> But when I want an array of weak references to the protocol I get an error:
>
> // Array of weak references of a protocol not OK
> let weakPs: [WeakReference] = [WeakReference(value: cP)] // Using 'P'
> as a concrete type conforming to protocol 'AnyObject' is not supported
> print("P: \(weakPs[0].value!.i)") // 0
>
>
> Is there something I have missed?
>
> The error message, "Using 'P' as a concrete type conforming to protocol
> 'AnyObject' is not supported", implies that it is a temporary limitation of
> the compiler; is this going to be fixed? Should I lodge a bug report?
>
> Thanks in advance for any advice,
>
>   -- Howard.
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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 procedure for requesting improvements to the book?)

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 snuck 
into the release? Or does the Swift in Xcode predate an API freeze?

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


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.withUpdate 
method only makes sense if the key type of the Dictionary is another 
Dictionary, which seems … very unusual.

(To clarify, I wasn’t asking how to accomplish this; it’s pretty simple to 
write it either as an extension method or as a simple one-off ‘for’ loop. I was 
just making sure it wasn’t already in the standard library, since it seems an 
obvious thing to have.)

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


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

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) -> Dictionary {
var result = self
result[key] = value
return result
}
}


> On Aug 30, 2016, at 1:00 PM, swift-users-requ...@swift.org wrote:
> 
> [swift-users] Mapping a Dictionary to a Dictionary?

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


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 define a protocol with an associatedtype
and use a protocol extension to add this functionality to foo instead.
E.g.:

protocol bar {
associatedtype X
}

extension bar where X == Bool {
...
}

public struct foo: bar {
typealias X = T

...
}

On Tue, Aug 30, 2016 at 6:15 PM, Jens Alfke via swift-users <
swift-users@swift.org> wrote:

> 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 ‘Bool’”. Is that an error? The book doesn’t say that type constraints
> have to be to protocols; in fact the example in the section “Type
> Constraint Syntax” shows a type constraint that requires T to be a subclass
> of SomeClass.*
>
> OK, so I’ll find a protocol for “boolean-like” values that’s implemented
> by Bool. The Swift reference for `Bool` in Xcode doesn’t list anything.
> Swiftdoc.org shows a protocol `Boolean` that looks like exactly what I
> want, but my compiler’s never heard of it; is this something that snuck in
> post-3.0? If I switch Swiftdoc.org back to Swift 2.2, it shows an
> equivalent protocol called `BooleanType`, but if I try to use that, the
> compiler says "'BooleanType' has been renamed to ‘Bool’”.
>
> At this point I threw my laptop at the wall.
> Not really, but I started to miss the stability and simplicity of C++… :-p
>
> How the @$*% do I do this?
>
> —Jens
>
> * In fact the book never seems to talk about type constraints in generic
> type extensions. It covers each of the two concepts, but not how to use
> them together.
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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 other types are "nominal 
types" (they can be named, and their compatibility is determined by that name).





If you search various Swift resources (like the swift-evolution archive) for 
those terms you’ll find lots of interesting discussions.

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


[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 
‘Bool’”. Is that an error? The book doesn’t say that type constraints have to 
be to protocols; in fact the example in the section “Type Constraint Syntax” 
shows a type constraint that requires T to be a subclass of SomeClass.*

OK, so I’ll find a protocol for “boolean-like” values that’s implemented by 
Bool. The Swift reference for `Bool` in Xcode doesn’t list anything. 
Swiftdoc.org  shows a protocol `Boolean` that looks like 
exactly what I want, but my compiler’s never heard of it; is this something 
that snuck in post-3.0? If I switch Swiftdoc.org  back to 
Swift 2.2, it shows an equivalent protocol called `BooleanType`, but if I try 
to use that, the compiler says "'BooleanType' has been renamed to ‘Bool’”.

At this point I threw my laptop at the wall.
Not really, but I started to miss the stability and simplicity of C++… :-p

How the @$*% do I do this?

—Jens

* In fact the book never seems to talk about type constraints in generic type 
extensions. It covers each of the two concepts, but not how to use them 
together.___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[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 using the proper vocabulary here or 
not.)

Thanks for your thoughts,
Ray Fix
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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, 2016, at 1:58 PM, Erica Sadun  wrote:
> 
> 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 and code 
> 
> -- E
> 
> 
>> On Aug 29, 2016, at 3:07 AM, Ray Fix via swift-users  
>> wrote:
>> 
>> With Xcode playground markup, if I write a multiline Swift code example such 
>> as:
>> 
>> 
>> for item in collection {
>>  print(item)
>> }
>> 
>> 
>> The rendered markup it always has the header “Example”  
>> 
>> Is there any way to write multiline code, or modify a stylesheet so that the 
>> heading “Example” is supressed?
>> 
>> Thank you,
>> Ray Fix
>> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

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