Re: [swift-users] NSInvocation equivalent

2016-10-05 Thread Jens Alfke via swift-users

> On Oct 5, 2016, at 4:24 PM, Jordan Rose  wrote:
> 
> However, there are usually better answers than NSInvocation (both in Swift 
> and Objective-C). What are you actually trying to do?

Yeah, the only time I’d recommend using NSInvocation is when implementing a 
proxy object using -forwardInvocation:, but that's explicitly disallowed in 
Swift. In other situations invocations are generally too slow, awkward to use, 
and hard to debug.

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


Re: [swift-users] NSInvocation equivalent

2016-10-05 Thread Kwame Bryan via swift-users
If you could define your needs. Is it a one to one ? Or one to many
relationship. I would look into the Observer Design pattern.

On Oct 5, 2016 7:26 PM, "Jordan Rose via swift-users" 
wrote:

>
> > On Oct 5, 2016, at 14:43, Jens Alfke via swift-users <
> swift-users@swift.org> wrote:
> >
> >
> >> On Oct 5, 2016, at 1:50 PM, Bob Miller via swift-users <
> swift-users@swift.org> wrote:
> >>
> >>  This is a Swift3 newbie question that I’ve not found a solution
> to. Has there been any consensus reached on an equivalent approach to the
> AppKit class NSInvocation ? Here’s a simple objC example.
> >
> > Swift doesn’t have an equivalent because the language simply isn’t as
> dynamic as Objective-C: there is no mechanism to invoke a method at runtime
> given its name.
> >
> > If you’re on an Apple platform, and the method you want to call, and its
> class, are @objc — then you can literally use NSInvocation from Swift to do
> this. But not otherwise.
>
> However, there are usually better answers than NSInvocation (both in Swift
> and Objective-C). What are you actually trying to do?
>
> Jordan
>
> ___
> 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] NSInvocation equivalent

2016-10-05 Thread Jordan Rose via swift-users

> On Oct 5, 2016, at 14:43, Jens Alfke via swift-users  
> wrote:
> 
> 
>> On Oct 5, 2016, at 1:50 PM, Bob Miller via swift-users 
>>  wrote:
>> 
>>  This is a Swift3 newbie question that I’ve not found a solution to. Has 
>> there been any consensus reached on an equivalent approach to the AppKit 
>> class NSInvocation ? Here’s a simple objC example.
> 
> Swift doesn’t have an equivalent because the language simply isn’t as dynamic 
> as Objective-C: there is no mechanism to invoke a method at runtime given its 
> name.
> 
> If you’re on an Apple platform, and the method you want to call, and its 
> class, are @objc — then you can literally use NSInvocation from Swift to do 
> this. But not otherwise.

However, there are usually better answers than NSInvocation (both in Swift and 
Objective-C). What are you actually trying to do?

Jordan

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


Re: [swift-users] NSInvocation equivalent

2016-10-05 Thread Jens Alfke via swift-users

> On Oct 5, 2016, at 1:50 PM, Bob Miller via swift-users 
>  wrote:
> 
>   This is a Swift3 newbie question that I’ve not found a solution to. Has 
> there been any consensus reached on an equivalent approach to the AppKit 
> class NSInvocation ? Here’s a simple objC example.

Swift doesn’t have an equivalent because the language simply isn’t as dynamic 
as Objective-C: there is no mechanism to invoke a method at runtime given its 
name.

If you’re on an Apple platform, and the method you want to call, and its class, 
are @objc — then you can literally use NSInvocation from Swift to do this. But 
not otherwise.

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


[swift-users] NSInvocation equivalent

2016-10-05 Thread Bob Miller via swift-users
Hello Swift Users,

This is a Swift3 newbie question that I’ve not found a solution to. Has 
there been any consensus reached on an equivalent approach to the AppKit class 
NSInvocation ? Here’s a simple objC example.


-(NSInteger)totalCount
{
NSInteger totalCount = -1;
if ([self.representedObject respondsToSelector:@selector(totalCount)])
{
   SEL selector = @selector(totalCount);
NSMethodSignature  *aSignature = [[self.representedObject class] 
instanceMethodSignatureForSelector:selector];

if (aSignature != nil)
{
NSInvocation  *anInvocation = [NSInvocation 
invocationWithMethodSignature:aSignature];
[anInvocation setSelector:selector];
[anInvocation setTarget:self.representedObject];
[anInvocation invoke];
[anInvocation getReturnValue:];
}
}
return totalCount;
}

Thanks and regards,
RFM___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] A sample Rational number type

2016-10-05 Thread Hooman Mehr via swift-users
I encountered a bug in the standard library while working on an exact 
conversion from floating point types to my rational type 
 (when you 
pass a tolerance of zero)

SR-2868 : If the value of the floating 
point type is a power of two, its `significandWidth` property returns a 
nonsense value, because the behavior or Builtin.int_cttz_IntNN (used in 
countTrailingZeros property) is undefined for zero. 

The test suite should have included such special cases and caught this one.

> On Oct 3, 2016, at 9:08 AM, Dave Abrahams via swift-users 
>  wrote:
> 
> on Sun Oct 02 2016, Hooman Mehr  > wrote:
> 
>>> On Oct 2, 2016, at 5:23 PM, Dave Abrahams via swift-users 
>>> > wrote:
>>> Presumably you mean:

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


Re: [swift-users] Different behaviour when casting Float and Double to NSNumber

2016-10-05 Thread Jens Alfke via swift-users

> On Oct 5, 2016, at 11:36 AM, Lars-Jørgen Kristiansen  
> wrote:
> 
> And thought that i would receive a overflows when stored into 'Float' 
> error...?

It doesn’t overflow. (I think the maximum value of Float is around 1e37.) There 
just isn’t enough precision to represent it exactly, which is pretty common; 
the usual example is that 0.1 can’t be represented exactly by any size binary 
floating-point.

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


Re: [swift-users] Different behaviour when casting Float and Double to NSNumber

2016-10-05 Thread Lars-Jørgen Kristiansen via swift-users
Thanks I will give that a try on Friday when I have the hardware available! 
Posted an issue and will add to that when I have tried, or if the library 
authors can comment on how they use the NSNumber..

> 5. okt. 2016 kl. 19.31 skrev Joe Groff :
> 
> 
>> On Oct 5, 2016, at 2:30 AM, Lars-Jørgen Kristiansen via swift-users 
>> > wrote:
>> 
>> I'm working with a third party API for some external hardware. One of the 
>> functions takes a NSNumber, and it fails to interact correctly with the 
>> hardware if I cast a Float too NSNumber, but works as expected if I use 
>> Double..
>> 
>> I dont know if it is related to NSNumber.stringValue since I dont know what 
>> the third part lib does with the NSNumber, but I noticed this:
>> 
>> let float = 100_000_00 as Float
>> let floatNumber = float as NSNumber
>> 
>> let double = 100_000_00 as Double
>> let doubleNumer = double as NSNumber
>> 
>> hardware.doThing(number: floatNumber as NSNumber) // Hardware does not work
>> hardware.doThing(number: doubleNumer as NSNumber) // Hardware works
>> 
>> // Also noticed this:
>> "\(floatNumber)" // "1e+07"
>> "\(doubleNumer)" // "1000"
>> 
>> Is this expected behaviour?
> 
> cc'ing Tony and Philippe, who might be able to comment on the intended 
> behavior of NSNumber here. When you cast using `as`, Swift will do the 
> conversion through its Objective-C bridge, and will attempt to use a custom 
> subclass of NSNumber that remembers the exact Swift type it was bridged as, 
> such as Float or Double. There's a small chance your library is making 
> assumptions that it's working with one of the standard NSNumber subclasses 
> used by the Foundation implementation. If you call the NSNumber initializer 
> `NSNumber(value: floatNumber)` instead of using the `as` conversion, you'll 
> get one of these standard NSNumbers instead of a Swift-bridged NSNumber. Try 
> that and see if it works. If using the initializer does indeed fix the 
> problem, we'd appreciate a bug report, so we can investigate fixing the 
> incompatibility with our NSNumber subclasses.
> 
> -Joe

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


Re: [swift-users] Different behaviour when casting Float and Double to NSNumber

2016-10-05 Thread Joe Groff via swift-users

> On Oct 5, 2016, at 2:30 AM, Lars-Jørgen Kristiansen via swift-users 
>  wrote:
> 
> I'm working with a third party API for some external hardware. One of the 
> functions takes a NSNumber, and it fails to interact correctly with the 
> hardware if I cast a Float too NSNumber, but works as expected if I use 
> Double..
> 
> I dont know if it is related to NSNumber.stringValue since I dont know what 
> the third part lib does with the NSNumber, but I noticed this:
> 
> let float = 100_000_00 as Float
> let floatNumber = float as NSNumber
> 
> let double = 100_000_00 as Double
> let doubleNumer = double as NSNumber
> 
> hardware.doThing(number: floatNumber as NSNumber) // Hardware does not work
> hardware.doThing(number: doubleNumer as NSNumber) // Hardware works
> 
> // Also noticed this:
> "\(floatNumber)" // "1e+07"
> "\(doubleNumer)" // "1000"
> 
> Is this expected behaviour?

cc'ing Tony and Philippe, who might be able to comment on the intended behavior 
of NSNumber here. When you cast using `as`, Swift will do the conversion 
through its Objective-C bridge, and will attempt to use a custom subclass of 
NSNumber that remembers the exact Swift type it was bridged as, such as Float 
or Double. There's a small chance your library is making assumptions that it's 
working with one of the standard NSNumber subclasses used by the Foundation 
implementation. If you call the NSNumber initializer `NSNumber(value: 
floatNumber)` instead of using the `as` conversion, you'll get one of these 
standard NSNumbers instead of a Swift-bridged NSNumber. Try that and see if it 
works. If using the initializer does indeed fix the problem, we'd appreciate a 
bug report, so we can investigate fixing the incompatibility with our NSNumber 
subclasses.

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


Re: [swift-users] Different behaviour when casting Float and Double to NSNumber

2016-10-05 Thread Jens Alfke via swift-users

> On Oct 5, 2016, at 2:30 AM, Lars-Jørgen Kristiansen via swift-users 
>  wrote:
> 
> // Also noticed this:
> "\(floatNumber)" // "1e+07"
> "\(doubleNumer)" // "1000"

Numbers around 10 million are too large to be represented exactly by a 32-bit 
float — the mantissa is 24 bits, including sign, so its range is ±8.3 million. 
(The specific number 10,000,000 does come out exactly, though, since it’s a 
multiple of 128.) 

So even if Float.description did use non-scientific notation for numbers at 
this scale, they wouldn’t be accurate. In fact the implementor of the 
.description method may have decided intentionally to switch to scientific 
notation at this scale so that the number of significant figures can be limited 
to the available precision.

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


Re: [swift-users] Identity based object hash?

2016-10-05 Thread Philippe Hausler via swift-users
ObjectIdentifier(self).hashValue is a decent approach; it hashes based upon the 
pointer address. That is how swift-corelibs-foundation does it for the swift 
implementation of NSObject: 
https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSObject.swift#L94

> On Oct 5, 2016, at 9:53 AM, Travis Griggs via swift-users 
>  wrote:
> 
> I have a class that I’d rather not have based off of NSObject, but I do want 
> to have identity based equality. I’ve done that as follows:
> 
> class Foobar { }
> 
> extension Foobar:Equatable { }
> 
> func == (a:Foobar, b:Foobar) -> Bool {
>   return a === b
> }
> 
> What’s less clear to me is how to go about implementing an identity based hash
> 
> extension Foobar:Hashable {
>   var hashValue:Int {
>   // what magic should happen 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] Linux - Calendar date(byAdding:to:wrappingComponents:) returns nil when it shouldn't?

2016-10-05 Thread Jason Ji via swift-users
Hello,

I'm having an issue with (NS)Calendar on Linux which I think is a bug, but
I just wanted to check first if it was just me or if this is indeed a bug.
I've filed a bug report here, just in case:
https://bugs.swift.org/browse/SR-2846

In short, Calendar has a method date(byAdding:to:wrappingComponents:) which
returns a new date which is the result of date arithmetic on the passed-in
date. It works fine on El Capitan, but doesn't seem to work properly on
Ubuntu 14.04. Below is some sample code:

import Foundation

let today = Date()
let diffComponents = DateComponents(day: -1)
let newDate = Calendar.current.date(byAdding: diffComponents, to:
today)  //returns nil


I've tried this in the swift REPL on Ubuntu 14.04 with both Swift
3.0-RELEASE, and the latest snapshot (October 2).

If anyone else could try this out as a sanity check for me, that would be
great - I'd be happy to be embarrassed that I've done something wrong.

Thanks,

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


[swift-users] Different behaviour when casting Float and Double to NSNumber

2016-10-05 Thread Lars-Jørgen Kristiansen via swift-users
I'm working with a third party API for some external hardware. One of the 
functions takes a NSNumber, and it fails to interact correctly with the 
hardware if I cast a Float too NSNumber, but works as expected if I use Double..

I dont know if it is related to NSNumber.stringValue since I dont know what the 
third part lib does with the NSNumber, but I noticed this:

let float = 100_000_00 as Float
let floatNumber = float as NSNumber

let double = 100_000_00 as Double
let doubleNumer = double as NSNumber

hardware.doThing(number: floatNumber as NSNumber) // Hardware does not work
hardware.doThing(number: doubleNumer as NSNumber) // Hardware works

// Also noticed this:
"\(floatNumber)" // "1e+07"
"\(doubleNumer)" // "1000"

Is this expected behaviour?___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users