Re: [swift-users] Can't make an escaping closure argument optional?

2018-01-05 Thread Kevin Nattinger via swift-users
If you remove the @escaping you'll notice it doesn't complain about a 
non-escaping closure escaping.
I could be wrong, but I believe that's because using it as an associated value 
forces it to escape on the calling side.

func esc(_ x: @escaping () -> ()) {
x()
}
func noesc(_ x: () -> ()) {
x()
}

func foo() {
noesc {
print(owner) // compiles
}
esc {
print(owner) // error: requires explicit 'self.'…
}
Optional<()->()>.some {
print(owner) // error: requires explicit 'self.'…
}
}


> On Jan 5, 2018, at 11:48 AM, Kenny Leung via swift-users 
>  wrote:
> 
> Hi All.
> 
> It seems that if you have an escaping closure argument, you can’t make it 
> optional. Am I right?
> init (
> owner:AnyObject,
> handler:@escaping (HXObserverNotification)->Void
> ) {
> self.owner = owner
> self.handler = handler
> }
> 
> You could try this:
> init (
> owner:AnyObject,
> handler:@escaping ((HXObserverNotification)->Void)?
> ) {
> self.owner = owner
> self.handler = handler
> }
> You get “@escaping attribute only applies to function types”
> 
> Or you could try this:
> init (
> owner:AnyObject,
> handler:(@escaping (HXObserverNotification)->Void)?
> ) {
> self.owner = owner
> self.handler = handler
> }
> You get “@escaping attribute may only be used in function parameter position”
> 
> -Kenny
> 
> 
> ___
> 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] dealing with heterogenous lists/dictionary with Codable

2017-10-19 Thread Kevin Nattinger via swift-users
So, what I see as the problem here is contradictory compiler statements:

MyPlayground.playground:5:9: note: cannot automatically synthesize 'Encodable' 
because '[String : Any]' does not conform to 'Encodable'
let someDict: [String : Any]
^

warning: MyPlayground.playground:8:18: warning: 'is' test is always true
[String : Any]() is Codable
 ^

So is it codable or isn't it? You can use a [String: Any] in encode() and 
decode() directly, so why doesn't it get compiler support? At the least, the 
message needs to be updated to something that's actually true. 

> On Oct 19, 2017, at 1:38 PM, David Sweeris via swift-users 
>  wrote:
> 
> Oh! Yeah, my bad... You are correct; I'd started thinking like I was on 
> -evolution instead of -users.
> 
> 
>> On Oct 19, 2017, at 1:29 PM, Itai Ferber > > wrote:
>> 
>> Mm, the point I’m trying to get at here is that JSON is inherently untyped 
>> while Swift is strongly typed, and the two don’t line up.
>> It doesn’t really make sense to ask to decode an existential because there’s 
>> not enough type information to influence what you get back.
>> 
>> On 19 Oct 2017, at 13:20, David Sweeris wrote:
>> 
>> I think if you can figure that out, you’re halfway to letting protocols 
>> conform to themselves.
>> 
>> (Syntactically, I would probably say that something like “Codable.Self” 
>> would read well, but I think that already means something. Maybe the answer 
>> will become clearer when we rework the reflection APIs?)
>> 
>> Sent from my iPhone
>> 
>> On Oct 19, 2017, at 13:13, Itai Ferber > > wrote:
>> 
>>> Even then, that wouldn’t necessarily help in the general case. If you 
>>> decode {"key" : 1} as [String : Codable], what concrete type would 1 have? 
>>> Int? Double? Int8? (Arguments can be made for any one of these, but the key 
>>> here is that it is inherently ambiguous and there isn’t necessarily a good 
>>> answer.)
>>> 
>>> On 19 Oct 2017, at 12:57, David Sweeris wrote:
>>> 
>>> On Oct 19, 2017, at 12:50 PM, David Baraff via swift-users 
>>> > wrote:
>>> 
>>> Yes; this is a case where anywhere in the code base I want to just say
>>> struct MyNewType : Codable {
>>> // add codable datatypes
>>> }
>>> 
>>> and don’t want/can’t always go to the centralized place to add it in.
>>> Is there some extension-like trick I can pull off that lets me spread the 
>>> implementation out over different files/libraries?
>>> 
>>> Ah, ok.
>>> 
>>> No, I don't think you'll be able to do that until/unless Swift gets more 
>>> macro/metaprogramming features. Maybe if protocols ever get to conform to 
>>> themselves? That's a common request, but implementing it is apparently 
>>> beyond tricky. I'm pretty sure somebody's working on it, but "bigger fish" 
>>> and all that...
>>> 
>>> - Dave Sweeris
>>> 
>> 
> 
> ___
> 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] question on generics

2017-02-22 Thread Kevin Nattinger via swift-users
This seems to work in a playground:

import CoreData

protocol SomeProtocol {}
class Foo where T: SomeProtocol {}

> On Feb 22, 2017, at 4:08 PM, Dave Reed via swift-users 
>  wrote:
> 
> I suspect this can't be done (at least not right now), but wanted to check.
> 
> I'd like to declare a class as a generic that meets a protocol and is also a 
> subclass of some specific type.
> 
> Something like class Foo (i.e., the T must 
> be both a NSManagedObject or subclass of it and conform to SomeProtocol).
> 
> Is this possible?
> 
> Thanks,
> Dave Reed
> 
> ___
> 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] default struct initializer internal even if class declared public

2017-01-18 Thread Kevin Nattinger via swift-users
I ran into this issue not half an hour ago; I would also prefer the default 
initializer to default to the entity’s access level, or at least have some 
simple way of opting in.

> On Jan 18, 2017, at 3:33 PM, Dave Reed via swift-users 
>  wrote:
> 
> I’m teaching an iOS with Swift this semester and one of my students pointed 
> out that:
> 
> struct Person {
>   var firstName: String
>   var lastName: String
> }
> 
> does create a default initializer that you can call as:
> p = Person(firstName: “Dave”, lastName: “Reed”)
> 
> but if you write:
> 
> public struct Person {
>   var firstName: String
>   var lastName: String
> }
> 
> The default initializer is still internal so if you want it to be public, you 
> have to write it yourself (i.e.)
> 
> public struct Person {
>   var firstName: String
>   var lastName: String
> 
>   public init(firstName: String, lastName: String) {
>  self.firstName = firstName
>  self.lastName = lastName
>   }
> }
> 
> Is there a way around this (other than writing it)? We both agree it would be 
> reasonable/nice that the default initializer have the same protection level 
> as the struct itself.
> 
> Thanks,
> Dave Reed
> 
> ___
> 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] Weird protocol behaviour.

2016-12-22 Thread Kevin Nattinger via swift-users
I recall seeing a request on the -evolution list for something like `T := X` to 
indicate it could be X itself or anything inheriting / implementing it, so it’s 
certainly known behavior, if not desired. IMO it’s a bug and `:` should be 
fixed to include the root type, whether or not that requires a discussion on 
-evolution.

> On Dec 22, 2016, at 2:17 PM, Howard Lovatt via swift-users 
>  wrote:
> 
> I suspect a compiler bug since A is a P. The equivalent in Java works:
> 
> interface P {}
> class X implements P {}
>  
>  void foo(A x) {}
>  
> void bar() {
> final P x = new X();
> foo(x);
> }
> 
> -- Howard. 
> 
> On 23 Dec 2016, at 3:19 am, Rien via swift-users  > wrote:
> 
>> IMO the error message says it all:
>> 
>> Playground execution failed: error: MyPlayground8.playground:9:5: error: 
>> cannot invoke 'foo' with an argument list of type '(P)'
>>foo(x)
>>^
>> 
>> MyPlayground8.playground:9:5: note: expected an argument list of type '(A)'
>>foo(x)
>>^
>> 
>> I.e. you are passing in a protocol while the function is specified for a 
>> type.
>> Said other way: On which data do you expect the protocol to operate?
>> 
>> Regards,
>> Rien
>> 
>> Site: http://balancingrock.nl 
>> Blog: http://swiftrien.blogspot.com 
>> Github: http://github.com/Swiftrien 
>> Project: http://swiftfire.nl 
>> 
>> 
>> 
>> 
>>> On 22 Dec 2016, at 17:05, Mikhail Seriukov via swift-users 
>>> > wrote:
>>> 
>>> Hello community! I' wondering if somebody can explain this to me.
>>> Please take look at the snippet.
>>> 
>>> protocol P {}
>>> struct X:P {}
>>> 
>>> func foo(_ x:A) {}
>>> 
>>> func bar() {
>>>//let x = X() // this compiles
>>>let x = X() as P // this does not compile. Why?
>>>foo(x)
>>> }
>>> 
>>> I expect the both cases to work though. But only first works? And I do not 
>>> understand why.
>>> My coworkers said that it is a compiler bug, but I'm not shure it is.
>>> Thanks for the help.
>>> ___
>>> 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

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


Re: [swift-users] Argument type 'Int' does not conform to expected type 'AnyObject'

2016-09-09 Thread Kevin Nattinger via swift-users
You’ll need to explicitly add the “as NSNumber” now.

https://github.com/apple/swift-evolution/blob/master/proposals/0072-eliminate-implicit-bridging-conversions.md
 


> On Sep 9, 2016, at 1:55 PM, Rick Mann via swift-users  
> wrote:
> 
> I've seen old (pre-Swift 3) posts online (e.g. 
> http://stackoverflow.com/questions/28920232/why-do-integers-not-conform-to-the-anyobject-protocol)
>  that address this, but my code worked yesterday before I used Xcode 8 GM to 
> migrate it to Swift 3, and now it doesn't, so I'm trying to understand what's 
> going on.
> 
> In this case, I have this code:
> 
> open class HttpServer
> {
>let clientSocketsLock = 0
> 
>open func start()
>{
>...some stuff...
>{
>HttpServer.lock(self.clientSocketsLock) //  ERROR HERE
>{
>self.clientSockets.remove(socket)
>}
>}
>}
> 
>open class func lock(_ handle: AnyObject, closure: () -> ())
>{
>objc_sync_enter(handle)
>closure()
>objc_sync_exit(handle)
>}
> }
> 
> I get the error "Argument type 'Int' does not conform to expected type 
> 'AnyObject'" at the line marked above. What has changed here?  Did Int's 
> behavior change? Is that described somewhere? I tried googling "Swift 3 Int" 
> and variations without much luck.
> 
> I did see one discussion that talked about how when passing an Int to 
> AnyObject, it was implicitly converted to NSNumber, and that this behavior 
> only applied to certain types (e.g. not Int32). How can I tell when this kind 
> of behavior exists? Is there a way to look at the type's declaration to see 
> (e.g. it conforms to a protocol, or has a compiler attribute, or something)?
> 
> Thanks,
> 
> -- 
> Rick Mann
> rm...@latencyzero.com
> 
> 
> ___
> 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] generic function called recursively not compiling

2016-08-04 Thread Kevin Nattinger via swift-users
Looks like a bug in the type inference system to me, but I’ll let someone with 
a better knowledge of the compiler confirm. Meanwhile, you can work around by 
explicitly casting any of the pieces of the return statement to `[Element]` or 
using a temporary for one or more of them.

let middle = [pivot]
return quickSort(left) + middle + quickSort(right)

return quickSort(left) as [Element] + [pivot] + quickSort(right)

return quickSort(left) + ([pivot] as [Element]) + quickSort(right)


> On Aug 4, 2016, at 9:02 AM, Ray Fix via swift-users  
> wrote:
> 
> 
> I filed rdar://27700622  and attached a playground.  Any 
> workaround magic I can do here?
> 
> func quickSort(_ input: [Element]) -> [Element] {
> if input.count < 2 {
> return input
> }
> let pivot = input.first!
> let left = input.dropFirst().filter { $0 <= pivot }
> let right = input.dropFirst().filter { $0 > pivot }
> 
> // Does not compile with (Swift 3pre) Xcode 8 b1,2,3,4
> // Does compile with (Swift 2.2) Xcode 7.3
> return quickSort(left) + [pivot] + quickSort(right)
> }
> 
> quickSort([3,5,1,2])
> 
> Error:
> //Playground execution failed: error: quicksort.playground:11:22: error: 
> cannot convert value of type '[Element]' to expected argument type '[_]'
> //return quickSort(left) + [pivot] + quickSort(right)
> // ^~~~
> 
> 
> ___
> 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] Printing large hexadecimal values

2016-05-25 Thread Kevin Nattinger via swift-users
“%08x” to zero-pad to 8 characters, “%08X" for uppercase A–F

% cat main.swift
import Foundation
let n = 10597059
print(String(format: "%x", n))
print(String(format: "%08x", n))
print(String(format: "%08X", n))
% swift main.swift
a1b2c3
00a1b2c3
00A1B2C3
%

> On May 25, 2016, at 10:54 AM, Jens Alfke via swift-users 
>  wrote:
> 
> 
>> On May 25, 2016, at 10:16 AM, Ken Burgett via swift-users 
>>  wrote:
>> 
>> I wish to print a 64-bit unsigned integer as 8 hexadecimal digits, and I 
>> can't find any documentation on this.  Please advise.
> 
> print(String(format: “%llx”, n))  // typed from memory
> 
> —Jens
> ___
> 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