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

2016-09-01 Thread Zhao Xin via swift-users
No. I don't think what you so called principle should be applied here. For
example, I have a `class Fruit`, then I have a `class Apple:Fruit`. If they
are using different `hashValue` generating method, you suggest me to use
composition instead of inheritance?

Also, it is very common for subclass to override super class `hashValue`.
Supposing opposite, if we also have another class called `class
Banana:Fruit`, we may get the result that an `Apple` is equals to a
`Banana`, using `Fruit`,  just because they have the same `hashValue`.

If we stick to the super class `hashValue`, we may also not get the
differences between instances of a certain subclass. For example, we may
get the result that a `redApple` equals to a `greenApple`.

So in my option, if one instance equals to another instance, the foundation
should be that the `type(of:instance)` equals. If you want to enlarge the
type to their super class, you need to be careful, as they are not
guaranteed automatically.

Zhaoxin

On Fri, Sep 2, 2016 at 7:32 AM, Jordan Rose  wrote:

> The Liskov substitution principle
>  says that a
> B should always be able to be treated like an A. Your Set may *already* 
> contain
> Bs, even without them ever being statically typed as B. If you think A and
> B are unrelated types, you should be using composition rather than
> inheritance.
>
> If a subclass overrides hashValue, they must be in a position to affect ==
> as well, and it must work no matter which object is on the left-hand side.
> NSObject does this by having == call the isEqual(_:) method, but you still
> need to design your class hierarchy and isEqual(_:) methods carefully.
>
> Jordan
>
>
> On Sep 1, 2016, at 16:28, Zhao Xin  wrote:
>
> I believe if B inherits A, they are not the same type. So the rule doesn't
> apply here.
>
> Zhaoxin
>
> On Fri, Sep 2, 2016 at 7:02 AM, Nick Brook  wrote:
>
>> Hi Jordan,
>>
>> Thanks for the advice.
>>
>> What if a subclass does implement hashValue differently? It seems you are
>> saying a subclass should never override hashValue? Should Set not compare
>> elements with == instead of hashValue?
>>
>> Thanks
>> Nick
>>
>> M: +44 (0)7986 048 141
>> W: http://nickbrook.me
>>
>> On 1 Sep 2016, at 23:55, Jordan Rose  wrote:
>>
>>
>> On Sep 1, 2016, at 15:44, Zhao Xin via swift-users 
>> wrote:
>>
>> Hi Nick,
>>
>> Glad to help.
>>
>> but when using third party classes I don’t know if the hash values are
>>> comparable
>>>
>>
>> You can create an extension with a convenient init(:), which creates a
>> new instance of  the super class basing on the instance of the sub class.
>> That will guarantee the subtraction. Below code works in Xcode 7.3.1 with
>> Swift 2.2.
>>
>> import Foundation
>>
>> func ==(lhs: Foo, rhs: Foo) -> Bool {
>> return lhs.id == rhs.id
>> }
>>
>> class Foo:Hashable {
>> let id:Int
>> var hashValue: Int {
>> return id
>> }
>>
>> required init(_ id:Int) {
>> self.id = id
>> }
>> }
>>
>> class Bar:Foo {
>> override var hashValue: Int {
>> return id * 5
>> }
>> }
>>
>> 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) // error: cannot invoke 'subtract' with an
>> argument list of type '(Set)'
>> fooSet = fooSet.subtract(barSet as Set) // works, but not what we
>> want
>> fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }
>> /*
>>  Foo, id:7
>>  Foo, id:10
>>  Foo, id:9
>> */
>>
>>
>> This isn't really a sensible thing to do. The rules for Hashable require
>> that `a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue !=
>> b.hashValue` implies `a != b`. If you break these rules you're going to
>> have problems no matter what static types you're using.
>>
>> Upcasting from Set to Set is the most concise way to solve this
>> problem.
>>
>> Jordan
>>
>>
>>
>
>
___
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-09-01 Thread Jordan Rose via swift-users
The Liskov substitution principle 
 says that a B 
should always be able to be treated like an A. Your Set may already contain 
Bs, even without them ever being statically typed as B. If you think A and B 
are unrelated types, you should be using composition rather than inheritance.

If a subclass overrides hashValue, they must be in a position to affect == as 
well, and it must work no matter which object is on the left-hand side. 
NSObject does this by having == call the isEqual(_:) method, but you still need 
to design your class hierarchy and isEqual(_:) methods carefully.

Jordan


> On Sep 1, 2016, at 16:28, Zhao Xin  wrote:
> 
> I believe if B inherits A, they are not the same type. So the rule doesn't 
> apply here.
> 
> Zhaoxin
> 
> On Fri, Sep 2, 2016 at 7:02 AM, Nick Brook  > wrote:
> Hi Jordan,
> 
> Thanks for the advice.
> 
> What if a subclass does implement hashValue differently? It seems you are 
> saying a subclass should never override hashValue? Should Set not compare 
> elements with == instead of hashValue?
> 
> Thanks
> Nick
> 
> M: +44 (0)7986 048 141 
> W: http://nickbrook.me 
> 
>> On 1 Sep 2016, at 23:55, Jordan Rose > > wrote:
>> 
>>> 
>>> On Sep 1, 2016, at 15:44, Zhao Xin via swift-users >> > wrote:
>>> 
>>> Hi Nick,
>>> 
>>> Glad to help.
>>> 
>>> but when using third party classes I don’t know if the hash values are 
>>> comparable
>>> 
>>> You can create an extension with a convenient init(:), which creates a new 
>>> instance of  the super class basing on the instance of the sub class. That 
>>> will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift 
>>> 2.2.
>>> 
>>> import Foundation
>>> 
>>> func ==(lhs: Foo, rhs: Foo) -> Bool {
>>> return lhs.id == rhs.id
>>> }
>>> 
>>> class Foo:Hashable {
>>> let id:Int
>>> var hashValue: Int {
>>> return id
>>> }
>>> 
>>> required init(_ id:Int) {
>>> self.id = id
>>> }
>>> }
>>> 
>>> class Bar:Foo {
>>> override var hashValue: Int {
>>> return id * 5
>>> }
>>> }
>>> 
>>> 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) // error: cannot invoke 'subtract' with an 
>>> argument list of type '(Set)'
>>> fooSet = fooSet.subtract(barSet as Set) // works, but not what we want
>>> fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }
>>> /*
>>>  Foo, id:7
>>>  Foo, id:10
>>>  Foo, id:9
>>> */
>> 
>> This isn't really a sensible thing to do. The rules for Hashable require 
>> that `a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue != 
>> b.hashValue` implies `a != b`. If you break these rules you're going to have 
>> problems no matter what static types you're using.
>> 
>> Upcasting from Set to Set is the most concise way to solve this 
>> problem.
>> 
>> Jordan
> 
> 

___
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-09-01 Thread Zhao Xin via swift-users
I believe if B inherits A, they are not the same type. So the rule doesn't
apply here.

Zhaoxin

On Fri, Sep 2, 2016 at 7:02 AM, Nick Brook  wrote:

> Hi Jordan,
>
> Thanks for the advice.
>
> What if a subclass does implement hashValue differently? It seems you are
> saying a subclass should never override hashValue? Should Set not compare
> elements with == instead of hashValue?
>
> Thanks
> Nick
>
> M: +44 (0)7986 048 141
> W: http://nickbrook.me
>
> On 1 Sep 2016, at 23:55, Jordan Rose  wrote:
>
>
> On Sep 1, 2016, at 15:44, Zhao Xin via swift-users 
> wrote:
>
> Hi Nick,
>
> Glad to help.
>
> but when using third party classes I don’t know if the hash values are
>> comparable
>>
>
> You can create an extension with a convenient init(:), which creates a new
> instance of  the super class basing on the instance of the sub class. That
> will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift
> 2.2.
>
> import Foundation
>
> func ==(lhs: Foo, rhs: Foo) -> Bool {
> return lhs.id == rhs.id
> }
>
> class Foo:Hashable {
> let id:Int
> var hashValue: Int {
> return id
> }
>
> required init(_ id:Int) {
> self.id = id
> }
> }
>
> class Bar:Foo {
> override var hashValue: Int {
> return id * 5
> }
> }
>
> 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) // error: cannot invoke 'subtract' with an
> argument list of type '(Set)'
> fooSet = fooSet.subtract(barSet as Set) // works, but not what we
> want
> fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }
> /*
>  Foo, id:7
>  Foo, id:10
>  Foo, id:9
> */
>
>
> This isn't really a sensible thing to do. The rules for Hashable require
> that `a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue !=
> b.hashValue` implies `a != b`. If you break these rules you're going to
> have problems no matter what static types you're using.
>
> Upcasting from Set to Set is the most concise way to solve this
> problem.
>
> Jordan
>
>
>
___
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-09-01 Thread Zhao Xin via swift-users
>
> `A hash value, provided by a type’s hashValue property, is an integer
> that is the same for any two instances that compare equally. That is, for
> two instances a
> ​​
> and b of the same type`


So do you believe A and B are the same type if B inherits A. That's the
differences between you and me.

Zhaoxin

On Fri, Sep 2, 2016 at 6:55 AM, Jordan Rose  wrote:

>
> On Sep 1, 2016, at 15:44, Zhao Xin via swift-users 
> wrote:
>
> Hi Nick,
>
> Glad to help.
>
> but when using third party classes I don’t know if the hash values are
>> comparable
>>
>
> You can create an extension with a convenient init(:), which creates a new
> instance of  the super class basing on the instance of the sub class. That
> will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift
> 2.2.
>
> import Foundation
>
> func ==(lhs: Foo, rhs: Foo) -> Bool {
> return lhs.id == rhs.id
> }
>
> class Foo:Hashable {
> let id:Int
> var hashValue: Int {
> return id
> }
>
>
> required init(_ id:Int) {
> self.id = id
> }
> }
>
> class Bar:Foo {
> override var hashValue: Int {
> return id * 5
> }
> }
>
> 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) // error: cannot invoke 'subtract' with an
> argument list of type '(Set)'
> fooSet = fooSet.subtract(barSet as Set) // works, but not what we
> want
> fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }
> /*
>  Foo, id:7
>  Foo, id:10
>  Foo, id:9
> */
>
>
> This isn't really a sensible thing to do. The rules for Hashable require
> that `a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue !=
> b.hashValue` implies `a != b`. If you break these rules you're going to
> have problems no matter what static types you're using.
>
> Upcasting from Set to Set is the most concise way to solve this
> problem.
>
> Jordan
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Function to unsafe pointer and back

2016-09-01 Thread Andrew Trick via swift-users

> On Sep 1, 2016, at 12:37 PM, Lou Zell via swift-users  
> wrote:
> 
> As to your real question, what’s your high-level goal?  Swift doesn’t really 
> do pointers to functions [1] but it does provide lots of other excellent 
> ‘treat code as data’ features.  If you can explain more about your goal, 
> perhaps we can direct you to a better path.
> ​
> Curiosity got the better of me on this one - there's no higher level goal 
> other than improved understanding.  I was playing around with function 
> currying in the repl and that's what lead me to those experiments.  
> 
> The article, and the preceding one in the series, has plenty for me to work 
> with.  Thank you!

That’s an awesome article, but I don’t think you need to understand any of it! 
I’m not an expert in this either, but here’s what I can see from your code...

It looks like you’re trying to capture the address of a function that was JIT’d 
for the purpose of evaluating a statement in the repl. That address might not 
be valid in the next statement.

Also, in the first case the UnsafePointer points to local memory that holds the 
function value. In the second case, you’re trying to load a function value from 
the address of the function body. So a level of indirection is missing.

You can’t take an address to a function in Swift. You can assign a variable to 
the function, as you did in the first case:

var x = doNothing

And view the address of that variable as an argument, only for the duration of 
the call, as you did in the first case:

call(&x)

-Andy

___
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-09-01 Thread Howard Lovatt via swift-users
@Karl,

You say "In the second example, you’re creating WeakReference. P does
not conform to P or to AnyObject.", but P does conform to AnyObject.

I suspect it is a compiler limitation/ bug.

 -- Howard.

On Thursday, 1 September 2016, Karl  wrote:

>
> On 1 Sep 2016, at 03:23, Howard Lovatt via swift-users <
> swift-users@swift.org
> > 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 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
>
>
> Your problem is protocol self-conformance. In the first example, you’re
> creating WeakReference. CP conforms to P and to AnyObject. In the
> second example, you’re creating WeakReference. P does not conform to P
> or to AnyObject.
>
> As for why @objc fixes it? … ¯\_(ツ)_/¯ all bets are off whenever @objc
> gets involved in anything.
>
> Karl
>


-- 
-- Howard.
___
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-09-01 Thread Nick Brook via swift-users
Hi Jordan,

Thanks for the advice.

What if a subclass does implement hashValue differently? It seems you are 
saying a subclass should never override hashValue? Should Set not compare 
elements with == instead of hashValue?

Thanks
Nick

M: +44 (0)7986 048 141
W: http://nickbrook.me 
> On 1 Sep 2016, at 23:55, Jordan Rose  wrote:
> 
>> 
>> On Sep 1, 2016, at 15:44, Zhao Xin via swift-users > > wrote:
>> 
>> Hi Nick,
>> 
>> Glad to help.
>> 
>> but when using third party classes I don’t know if the hash values are 
>> comparable
>> 
>> You can create an extension with a convenient init(:), which creates a new 
>> instance of  the super class basing on the instance of the sub class. That 
>> will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift 
>> 2.2.
>> 
>> import Foundation
>> 
>> func ==(lhs: Foo, rhs: Foo) -> Bool {
>> return lhs.id == rhs.id
>> }
>> 
>> class Foo:Hashable {
>> let id:Int
>> var hashValue: Int {
>> return id
>> }
>> 
>> required init(_ id:Int) {
>> self.id = id
>> }
>> }
>> 
>> class Bar:Foo {
>> override var hashValue: Int {
>> return id * 5
>> }
>> }
>> 
>> 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) // error: cannot invoke 'subtract' with an 
>> argument list of type '(Set)'
>> fooSet = fooSet.subtract(barSet as Set) // works, but not what we want
>> fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }
>> /*
>>  Foo, id:7
>>  Foo, id:10
>>  Foo, id:9
>> */
> 
> This isn't really a sensible thing to do. The rules for Hashable require that 
> `a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue != 
> b.hashValue` implies `a != b`. If you break these rules you're going to have 
> problems no matter what static types you're using.
> 
> Upcasting from Set to Set is the most concise way to solve this 
> problem.
> 
> Jordan

___
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-09-01 Thread Jordan Rose via swift-users

> On Sep 1, 2016, at 15:44, Zhao Xin via swift-users  
> wrote:
> 
> Hi Nick,
> 
> Glad to help.
> 
> but when using third party classes I don’t know if the hash values are 
> comparable
> 
> You can create an extension with a convenient init(:), which creates a new 
> instance of  the super class basing on the instance of the sub class. That 
> will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift 
> 2.2.
> 
> import Foundation
> 
> func ==(lhs: Foo, rhs: Foo) -> Bool {
> return lhs.id == rhs.id
> }
> 
> class Foo:Hashable {
> let id:Int
> var hashValue: Int {
> return id
> }
> 
> required init(_ id:Int) {
> self.id = id
> }
> }
> 
> class Bar:Foo {
> override var hashValue: Int {
> return id * 5
> }
> }
> 
> 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) // error: cannot invoke 'subtract' with an argument 
> list of type '(Set)'
> fooSet = fooSet.subtract(barSet as Set) // works, but not what we want
> fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }
> /*
>  Foo, id:7
>  Foo, id:10
>  Foo, id:9
> */

This isn't really a sensible thing to do. The rules for Hashable require that 
`a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue != b.hashValue` 
implies `a != b`. If you break these rules you're going to have problems no 
matter what static types you're using.

Upcasting from Set to Set is the most concise way to solve this 
problem.

Jordan

___
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-09-01 Thread Zhao Xin via swift-users
Hi Nick,

Glad to help.

but when using third party classes I don’t know if the hash values are
> comparable
>

You can create an extension with a convenient init(:), which creates a new
instance of  the super class basing on the instance of the sub class. That
will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift
2.2.

import Foundation


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

return lhs.id == rhs.id

}


class Foo:Hashable {

let id:Int

var hashValue: Int {

return id

}



required init(_ id:Int) {

self.id = id

}

}


class Bar:Foo {

override var hashValue: Int {

return id * 5

}

}


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) // error: cannot invoke 'subtract' with an
argument list of type '(Set)'

fooSet = fooSet.subtract(barSet as Set) // works, but not what we want

fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }

/*

 Foo, id:7

 Foo, id:10

 Foo, id:9

*/


Extension part.  You should comment above subtracting code before you run
below code, as you want to make sure the result is not polluted.


extension Foo {

convenience init(_ instance:Foo) {

self.init(instance.id)

}

}


let anotherFooSet = { () -> Set in

var set = Set()

for element in barSet {

let foo = Foo(element)

set.insert(foo)

}



return set

}()


fooSet = fooSet.subtract(anotherFooSet)

fooSet.forEach { print("\($0.dynamicType), id:\($0.id)") }

/*

 Foo, id:10

 Foo, id:9

*/


Zhaoxin

On Thu, Sep 1, 2016 at 9:41 PM, Nick Brook  wrote:

> Hi Zhao
>
> Thanks for your response.
>
> I understand your point, but when using third party classes I don’t know
> if the hash values are comparable, but for example I may want to have a set
> of ‘data' (NSData) and a subset of ‘mutable data' (NSMutableData), which
> point to the same objects. As a user of swift I would expect to be able to
> subtract Set from Set.
>
> Your last example perhaps works in Swift 3, so this may be fixed now, but
> in Swift 2 you get the error
>
> Cannot invoke 'subtract' with an argument list of type '(Set)’
>
> Perhaps Swift 3 supports it with some additional safety around hashValue
> overriding or something.
>
> Thanks
>
> Nick
>
> On 1 Sep 2016, at 04:00, Zhao Xin  wrote:
>
> 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 fu

Re: [swift-users] DWARF without DSYM

2016-09-01 Thread Dmitry Shevchenko via swift-users
Hi all, thanks for all the pointers.

After some more research, I have found that without dSYM, Xcode uses
-add_ast_path to encode swiftmodule location.

It then seems like you can only have one AST tag per binary, or at least
only the first one is checked -
https://github.com/apple/swift-lldb/blob/d4114d1eb749963f81ee7e6f937e1ee27681381d/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp#L1602

What if a program is composed from multiple Swift modules, say A imports B
which imports C, we then have 3 swiftmodule files. Which one should be
referenced during the linking step, A? Undefined behavior? :)

On Fri, Aug 26, 2016 at 5:50 PM Jim Ingham  wrote:

> In any case where the .o files are temporary objects which the driver will
> delete when it's done, it has to generate a dSYM file before it deletes
> them.  But if the .o files belong to the user it can assume it's okay to
> hold off on generating the dSYM.  Same thing happens with the clang driver.
>
> Jim
>
> > On Aug 26, 2016, at 2:43 PM, Dmitry Shevchenko 
> wrote:
> >
> > Ah I see, the dsym job is only created when the driver will also link
> the final product, in Xcode build case, it separates the linking step.
> >
> > On Fri, Aug 26, 2016 at 5:35 PM Dmitry Shevchenko 
> wrote:
> > I experimented in Xcode, and with DWARF w/o dSYM selected, debugging
> still works. And even though -g option is passed to swiftc, there's no dSYM
> generation occurring. So besides -g, what else makes swiftc issues that
> dsymutil call?
> >
> > On Fri, Aug 26, 2016 at 3:37 PM Jim Ingham  wrote:
> > dsymutil is only given the .o files and the executable - same thing lldb
> sees.  So if it can find the module map to copy it into the dSYM, lldb can
> find it and load it without the dSYM.  So whether it does work or not, it
> should be able to.
> >
> > Jim
> >
> > > On Aug 26, 2016, at 11:37 AM, Jordan Rose via swift-users <
> swift-users@swift.org> wrote:
> > >
> > > I suppose it can, but in theory the module that goes into the dSYM
> wouldn't be the same as the one that gets used by clients of a library.
> (Example: the one in the dSYM needs to have info about private types.) Sean
> can probably explain better than I can.
> > >
> > > Jordan
> > >
> > >
> > >> On Aug 26, 2016, at 9:36, Dmitry Shevchenko 
> wrote:
> > >>
> > >> I see. I thought LLDB can import modules independently of sources,
> isn't that what target.swift-module-search-paths option is for?
> > >>
> > >> On Thu, Aug 25, 2016 at 4:15 PM Jordan Rose 
> wrote:
> > >> Plain DWARF isn't sufficient to debug a Swift program (we actually
> stuff the entire swiftmodule into the dSYM), but if you just want to trace
> execution you should be able to use -gline-tables-only.
> > >>
> > >> Jordan
> > >>
> > >>
> > >> > On Aug 25, 2016, at 13:10, Dmitry Shevchenko via swift-users <
> swift-users@swift.org> wrote:
> > >> >
> > >> > Can swiftc generate debug info without a separate dSYM bundle? -g
> option looks to always generate a dSYM.
> > >> > ___
> > >> > 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
> >
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Statically linked binaries on linux

2016-09-01 Thread Daniel Dunbar via swift-users
We don't currently build static versions of the other libraries (Foundation, 
XCTest), or a support process for picking them up.

This is covered by:
  https://bugs.swift.org/browse/SR-648

As you note, it largely works for Swift-only, so this is feasible but we need 
someone to drive it. Are you interested in working on it?

 - Daniel

> On Sep 1, 2016, at 9:13 AM, Joel Hughes via swift-users 
>  wrote:
> 
> Hi,
> 
> I'm attempting to get a statically linked binary and am running into errors.
> 
> I'm using Swift 3 Preview 6 on Ubuntu.
> 
> Regular _swift build_ and _swift test_ are all running fine.
> 
> I can produce a static binary for a simple "hello world" using:
> 
> swift build -c release -Xswiftc -static-stdlib
> 
> (although I do get error while loading shared libraries: libicui18n.so.55 
> when running in a basic vm).
> 
> However I can't compile a more complicated project. It's only dependency is 
> Foundation (it uses NSUUID, and JSON) and can't seem to find them.
> 
> I get a stream of errors, examples:
> 
> Linking ./.build/release/joke
> /usr/bin/ld.gold: error: cannot find -lFoundation
> ...
> error: undefined reference to '_TMaC10Foundation6NSUUID'
> ...
> error: undefined reference to 
> '_TFC10Foundation6NSUUIDCfT10uuidStringSS_GSqS0__'
> 
> Any pointers or advice much appreciated.
> 
> Thanks
> 
> Joel
> ___
> 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] Function to unsafe pointer and back

2016-09-01 Thread Lou Zell via swift-users
>
> As to your real question, what’s your high-level goal?  Swift doesn’t
> really do pointers to functions [1] but it does provide lots of other
> excellent ‘treat code as data’ features.  If you can explain more about
> your goal, perhaps we can direct you to a better path.
>
​
Curiosity got the better of me on this one - there's no higher level goal
other than improved understanding.  I was playing around with function
currying in the repl and that's what lead me to those experiments.

The article, and the preceding one in the series, has plenty for me to work
with.  Thank you!
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Statically linked binaries on linux

2016-09-01 Thread Joel Hughes via swift-users
Hi,

I'm attempting to get a statically linked binary and am running into errors.

I'm using Swift 3 Preview 6 on Ubuntu.

Regular _swift build_ and _swift test_ are all running fine.

I can produce a static binary for a simple "hello world" using:

swift build -c release -Xswiftc -static-stdlib

(although I do get error while loading shared libraries: libicui18n.so.55
when running in a basic vm).

However I can't compile a more complicated project. It's only dependency is
Foundation (it uses NSUUID, and JSON) and can't seem to find them.

I get a stream of errors, examples:

Linking ./.build/release/joke
/usr/bin/ld.gold: error: cannot find -lFoundation
...
error: undefined reference to '_TMaC10Foundation6NSUUID'
...
error: undefined reference to
'_TFC10Foundation6NSUUIDCfT10uuidStringSS_GSqS0__'

Any pointers or advice much appreciated.

Thanks

Joel
___
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-09-01 Thread Nick Brook via swift-users
Hi Zhao

Thanks for your response.

I understand your point, but when using third party classes I don’t know if the 
hash values are comparable, but for example I may want to have a set of ‘data' 
(NSData) and a subset of ‘mutable data' (NSMutableData), which point to the 
same objects. As a user of swift I would expect to be able to subtract 
Set from Set.

Your last example perhaps works in Swift 3, so this may be fixed now, but in 
Swift 2 you get the error

Cannot invoke 'subtract' with an argument list of type '(Set)’

Perhaps Swift 3 supports it with some additional safety around hashValue 
overriding or something.

Thanks

Nick

> On 1 Sep 2016, at 04:00, Zhao Xin  wrote:
> 
> 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 
> mailto: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] forEach error: Parameters may not have the 'var' specifier

2016-09-01 Thread Zhao Xin via swift-users
You issue is more like

func foo(var bar:Int) { // error: Parameters may not have the 'var'
specifier



}

So yes, `var` is not allow any more.

Removing var from Function Parameters (SE-0003)


Zhaoxin

On Thu, Sep 1, 2016 at 6:21 PM, CK TUNG via swift-users <
swift-users@swift.org> wrote:

> This error is for Swift 3 Xcode 8 beta 6
>
> array.forEach {( var a : AnyObject) -> () in // error here : Parameters
> may not have the 'var' specifier
>
>// modify object here
>
> }
>
>
> for var a in array {  // OK
>
> // modify object here
>
> }
>
>
>
> ___
> 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


[swift-users] forEach error: Parameters may not have the 'var' specifier

2016-09-01 Thread CK TUNG via swift-users

This error is for Swift 3 Xcode 8 beta 6

array.forEach {( var a : AnyObject) -> () in // error here : Parameters may not 
have the 'var' specifier
   // modify object here
}

for var a in array {  // OK
    // modify object here
}

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


[swift-users] Swift Strangeness? Failable initializer overriding non-failable vs. default parameters

2016-09-01 Thread Stefan via swift-users
Hi all,

recently I stumbled upon an interesting and slightly confusing aspect of our 
favorite language. 

I had defined a Cache class to conform to NSCoding and inherit from NSObject. 
My class’s initializer was declared init?(entries: [Entry]? = nil) at first, 
giving an option to pre-populate it when called from the NSCoding-conformant 
initializer but also simply start from scratch in my program. Failability is a 
valuable feature since the class depends on external resources that could be 
unavailable. Of course there are other means to model this, but a failable 
initializer is the most elegant one, I think.

Later on, I decided to make the Entry class private to my Cache class.

Now I had to split the initializer in a private init?(entries: [Entry]?) and an 
internal or public convenience init?().

I’ll abstract a bit now:


First version:

public class A: NSObject {
public class X { }
public init?(x: X? = nil) { }
}

— all good. I can use it like let a = A() in my program.

Second version:

public class B: NSObject {
private class X { }
private init?(x: X?) { }
public convenience override init?() { self.init(x: nil) }   // ERROR
}

Now the compiler complains "failable initializer 'init()' cannot override a 
non-failable initializer" with the overridden initializer being the public 
init() in NSObject. This is the same in Swift 2 and 3.
Omitting the override does not work, the compiler now says “overriding 
declaration requires an 'override’ keyword", so it does count as overriding 
anyway. Suggested Fix-it is inserting override, giving the other error.

How comes I can effectively declare an initializer A.init?() without the 
conflict but not B.init?() ?

And: Why at all am I not allowed to override a non-failable initializer with a 
failable one? The opposite is legal: I can override a failable initializer with 
a non-failable, which even requires using a forced super.init()! and thus 
introduces the risk of a runtime error. I always have a bad feeling when I use 
!, so I try to avoid it whenever possible. I would even accept to get a 
compiler warning ;-)

To me, letting the subclass have the failable initializer feels more natural 
since an extension of functionality introduces more chance of failure. But 
maybe I am missing something here – explanation greatly appreciated.

But I found a workaround. My class now looks like this:

public class C: NSObject {
private class X { }
private init?(x: X?) { }
public convenience init?(_: Void) { self.init(x: nil) } // NO 
ERROR
}

Now I can use it like let c = C(()) or even let c = C() – which is exactly what 
I intended at first.

The fact that the new declaration does not generate an error message seems 
(kind of) legal since it (somehow) differs from the superclass initializer's 
signature. Nevertheless, using a nameless Void parameter at all and then even 
omitting it completely in the 2nd version of the call feels a bit strange… But 
the end justifies the means, I suppose. Elegance is almost preserved.

What do you think?

Sincerely,
Stefan

PS: I already posted this question on Stack Overflow 
, but for the more philosophical 
aspects / underpinnings, it is not the right medium — you are obliged to ask a 
concrete question there (How to?, not Why?).___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Function to unsafe pointer and back

2016-09-01 Thread Quinn "The Eskimo!" via swift-users

On 1 Sep 2016, at 01:41, Lou Zell via swift-users  wrote:

> 3. What is a reabstraction thunk helper (do I want to know :))

This makes for a great read.



As to your real question, what’s your high-level goal?  Swift doesn’t really do 
pointers to functions [1] but it does provide lots of other excellent ‘treat 
code as data’ features.  If you can explain more about your goal, perhaps we 
can direct you to a better path.

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

[1] At least not natively.  You can use @convention(c) to interact with C APIs 
that require a function pointer.

___
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-09-01 Thread Quinn "The Eskimo!" via swift-users

On 31 Aug 2016, at 19:09, Jens Alfke via swift-users  
wrote:

> (Any doc writers listening? Or is there a procedure for requesting 
> improvements to the book?)

If you file a Radar bug against the doc it will go to the right folks [1].



> I’m also bemused …

This is a linear progression in time:

1. `BooleanType` is the name in Swift 2.

2. `Boolean` is the post ‘great renaming’ name in Swift 3.

3. Currently it is unavailable per SE-0109.

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

[1] Other stuff might works as well but Radar will definitely work.

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


[swift-users] [swift-corelibs-foundation] Tests not showing in Xcode test navigator

2016-09-01 Thread Dan Tugendraich via swift-users
I have performed all the getting started steps in; 
https://github.com/apple/swift-corelibs-foundation/blob/swift-3.0-branch/Docs/GettingStarted.md
Everything seems to be working fine, I can build the SwiftFoundation target and 
I can run the TestFoundation target. But one of the tests are failing in the 
test-suite. But I have no way of checking which test and why because the test 
navigator window in Xcode is empty. It wants me to add a new target, but I want 
it to show the already existing test target ”TestFoundation”. Any ideas on how 
to achieve this?
Dan Tugendraich

 


Kärleksgatan 2, 211 45 Malmö 
+46 (0) 40 330 480 
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users