Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Marco S Hyman via swift-users
> On Jul 7, 2017, at 9:48 PM, Zhao Xin wrote: > > Thank you very much Marco. But What is “outside of an initializer” really > bothers me. **Both** `func bar(keysAndValues:Dictionary)` > works now. **Are they really outside ?** Uhhh, that is certainly not

Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Zhao Xin via swift-users
Thank you very much Marco. But What is “outside of an initializer” really bothers me. **Both** `func bar(keysAndValues:Dictionary)` works now. **Are they really outside ?** struct Foo { var keys = ["z","y","x"] { didSet { keys.sort() }

Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Marco S Hyman via swift-users
> init(keysAndValues:Dictionary) { > self.keys.append(contentsOf: keysAndValues.keys) > } > > Above code doesn't call `didSet` in playground. My .swift file is similar and > didn't call `didSet` either. However, if without a struct, `didSet` is called. “If

Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Zhao Xin via swift-users
import Cocoa struct Foo { var keys = ["z","y","x"] { didSet { keys.sort() } } init(keysAndValues:Dictionary) { self.keys.append(contentsOf: keysAndValues.keys) } } let keysAndValues:Dictionary =

Re: [swift-users] How to add this generic static func as a protocol requirement?

2017-07-07 Thread Jens Persson via swift-users
That will not work since R can be different types depending on T (R == (T, T) for S2 and R == (T, T, T) for S3). I guess Swift would have to support generic associated type for it to work: protocol P { associatedtype R static func foo(_ v: T) -> R } struct S2 : P { typealias R = (U, U)

Re: [swift-users] How to add this generic static func as a protocol requirement?

2017-07-07 Thread Slava Pestov via swift-users
Try using an associated type for the result of foo(): protocol P { associatedtype R static func foo(_ v: T) -> R } Slava > On Jul 7, 2017, at 1:50 AM, Jens Persson via swift-users > wrote: > > protocol P { > // … > // For example the following will not

Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Jordan Rose via swift-users
It definitely should. Can you show the code where it wasn’t being called? Thanks! Jordan > On Jul 7, 2017, at 00:31, Zhao Xin via swift-users > wrote: > > Should Array's `append(_)` functions cause the array's `didSet`? > In my own test, it did call `didSet` in

Re: [swift-users] UserDefaults with generic keys

2017-07-07 Thread Thierry Passeron via swift-users
Nice! The key is to have a non generic base class as storage for statics and a subclass for generic types… brilliant. I am definitely going to try this one. Vladimir’s solution is also a nice fact to know. The key point of the compile error message seems to be « static __stored__ « hence,

Re: [swift-users] UserDefaults with generic keys

2017-07-07 Thread Kim Burgestrand via swift-users
Here's yet another alternative. I read an article doing this very thing a while back, it might be interesting to you: http://radex.io/swift/nsuserdefaults/static/. It makes the key type a class instead, and inherits from a non-generic parent class to which it adds the static properties. The gist

Re: [swift-users] UserDefaults with generic keys

2017-07-07 Thread Vladimir.S via swift-users
On 07.07.2017 14:02, Thierry Passeron via swift-users wrote: Hi Everyone, Using Swift 3.1, I was wondering if I could come up with something largely inspired by Notification.Name to help me deal with UserDefaults so I started by doing something like: The only kind of solution I was able to

Re: [swift-users] did i just imagine tuples were going to be hashable in swift 4?

2017-07-07 Thread Vladimir.S via swift-users
On 07.07.2017 5:50, David Baraff via swift-users wrote: I thought I read that tuples would finally be hashable in swift 4, allowing for them to be used in dictionaries/sets, but now that i google for it, i find no mention of it. are there any plans? so often i just want to quickly create a

[swift-users] UserDefaults with generic keys

2017-07-07 Thread Thierry Passeron via swift-users
Hi Everyone, Using Swift 3.1, I was wondering if I could come up with something largely inspired by Notification.Name to help me deal with UserDefaults so I started by doing something like: public struct DefaultsKey: RawRepresentable, Equatable, Hashable, Comparable { public var rawValue:

Re: [swift-users] did i just imagine tuples were going to be hashable in swift 4?

2017-07-07 Thread Adrian Zubarev via swift-users
Welcome to the group of people who thinks that the character from Monopoly had a monocle, including myself. You've just experienced a transdimensional jump from your universe without even notecing, but where Swift 4 actually had hashable tuples. :) Jokes aside, I haven't read anything about it

[swift-users] How to add this generic static func as a protocol requirement?

2017-07-07 Thread Jens Persson via swift-users
protocol P { // … // For example the following will not work: // static func foo(_ v: T) -> R // Is there some other way? // … } struct S2 : P { static func foo(_ v: T) -> (T, T) { return (v, v) } } struct S3 : P { static func foo(_ v: T) -> (T, T, T)

[swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Zhao Xin via swift-users
Should Array's `append(_)` functions cause the array's `didSet`? In my own test, it did call `didSet` in Playground. But in .swift files, it didn't call. Is this a known bug or something? Which is correct? Xcode Version 9.0 beta 2 (9M137d)​ swift --version Apple Swift version 4.0