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 the results I’d have expected.  Perhaps one of the  
swift language lawyers can explain.


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


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()

}

}



init(keysAndValues:Dictionary) {

func bar(keysAndValues:Dictionary) {

keys.append(contentsOf: keysAndValues.keys)

}

bar(keysAndValues:keysAndValues)

}



//private mutating func bar(keysAndValues:Dictionary) {

//keys.append(contentsOf: keysAndValues.keys)

//}

}


let keysAndValues:Dictionary = ["c":"c", "b":"b", "a":"a"]

var foo = Foo(keysAndValues: keysAndValues) // `let foo` is the same result


foo.keys.forEach { print($0) }

/*

 prints

 a

 b

 c

 x

 y

 z

 */


Zhao Xin

On Sat, Jul 8, 2017 at 9:59 AM, Marco S Hyman  wrote:

>
> > 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 you don’t need to compute the property but still need to provide code
> that is run before and after setting a new value, use willSet and didSet.
> The code you provide is run any time the value changes outside of an
> initializer.”
>
> Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).”
> iBooks. https://itun.es/us/jEUH0.l
>
> Note “outside of an initializer”.  didSet and willSet are not called in
> initializers.
>
>
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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 you don’t need to compute the property but still need to provide code that 
is run before and after setting a new value, use willSet and didSet. The code 
you provide is run any time the value changes outside of an initializer.”

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).” iBooks. 
https://itun.es/us/jEUH0.l

Note “outside of an initializer”.  didSet and willSet are not called in 
initializers.



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


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 = ["c":"c", "b":"b", "a":"a"]

var foo = Foo(keysAndValues: keysAndValues) // `let foo` is the same result


foo.keys.forEach { print($0) }

/*

 prints

 z

 y

 x

 b

 c

 a

*/


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.


var keys = ["z","y","x"]

{

didSet {

keys.sort()

}

}


let keysAndValues:Dictionary = ["c":"c", "b":"b", "a":"a"]

keys.append(contentsOf: keysAndValues.keys)



keys.forEach { print($0) }

/*

 prints

 a

 b

 c

 x

 y

 z

 */



*Xcode 9.0 beta 2 (9M137d)*


*Apple Swift version 4.0 (swiftlang-900.0.45.6 clang-900.0.26)*

*Target: x86_64-apple-macosx10.9*


*macOS Sierra 10.12.5 (16F73)*



Zhao Xin



On Fri, Jul 7, 2017 at 11:52 PM, Jordan Rose  wrote:

> 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 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 (swiftlang-900.0.45.6 clang-900.0.26)
> Target: x86_64-apple-macosx10.9
>
>
> Zhao Xin
> ___
> 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] 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)
static func foo(_ v: T) -> R {
return (v, v)
}
}
struct S3 : P {
typealias R = (U, U, U)
static func foo(_ v: T) -> R {
return (v, v, v)
}
}

But (sadly) according to previous discussions it seems like generic
associated types are very much out of scope for Swift 4.

PS

The generic static func was only a (what I guess is another) failed attempt
at a workaround, and what I'm really trying to achieve is something that is
perhaps better described like this:

protocol VectorCount {
associatedtype Storage
}
…
enum VectorCount2 : VectorCount {
typealias Storage = (E, E)
…
}
enum VectorCount3 : VectorCount {
typealias Storage = (E, E, E)
…
}
…
struct Vector {
var elements: Count.Storage
…
}

That is, Vector is a statically allocated array type, with type level Count
and Element as type parameters.
I currently have a solution which is based around separate generic vector
types for each VectorCountX type, but they are generic only over Element
not both Count and Element, like this:
…
protocol VectorProtocol {
associatedtype Count: VectorCount
associatedtype Element
…
}
struct V2 : VectorProtocol {
typealias Count = VectorCount2
var elements: (Element, Element)
…
}
struct V3 : VectorProtocol {
typealias Count = VectorCount3
var elements: (Element, Element, Element)
…
}
…
And you can of course also make specific types like eg:
struct RgbaFloatsSrgbGamma : Vector {
typealias Count = VectorCount4
typealias Element = Float
…
}

It's working and it can be written in a way such that the optimizer can do
a good job, vectorizing operations on vectors of 4 floats etc. It also
makes it possible to write generic Table where Index is an
N-dimensional vector of Int elements, which enables me to write
N-dimensional data processing generically, eg a calculating a summed area
table for data of any dimension, etc.  But it would be much better to base
the vector types around a concept in which both Element and Count are type
parameters, and not just Element, ie Vector rather than
V1, V2, …

/Jens


On Fri, Jul 7, 2017 at 6:49 PM, Slava Pestov  wrote:

> 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 <
> swift-users@swift.org> wrote:
> >
> > 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) {
> > return (v, v, v)
> > }
> > }
> >
> > (I'm guessing but am not sure that I've run into (yet) a(nother)
> situation which would require higher kinded types?)
> > ___
> > 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] 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 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) {
> return (v, v, v)
> }
> }
> 
> (I'm guessing but am not sure that I've run into (yet) a(nother) situation 
> which would require higher kinded types?)
> ___
> 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] 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 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 (swiftlang-900.0.45.6 clang-900.0.26)
> Target: x86_64-apple-macosx10.9
> 
> 
> Zhao Xin
> ___
> 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] 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, with computed it works.

Thanks for the posting.

> Le 7 juil. 2017 à 15:54, Kim Burgestrand  a écrit :
> 
> 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 of it is roughly like this (although the article uses subscript, 
> which does not allow for a generic implementation so I use a get/set approach 
> here for brevity):
> 
> ```
> class DefaultsKeys {}
> final class DefaultsKey: DefaultsKeys {
> let value: String
> 
> init(_ value: String) {
> self.value = value
> }
> }
> 
> extension UserDefaults {
> func get(_ key: DefaultsKey) -> T? {
> return object(forKey: key.value) as? T
> }
> 
> func set(_ key: DefaultsKey, to value: T) {
> set(value, forKey: key.value)
> }
> }
> 
> extension DefaultsKeys {
> static let version = DefaultsKey("version")
> }
> 
> let defaults = UserDefaults.standard
> defaults.set(.version, to: "1.0")
> let version = defaults.get(.version)
> print(version ?? "N/A")
> ```
> 
> On Fri, 7 Jul 2017 at 14:00 Vladimir.S via swift-users  > wrote:
> 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 implement, is with calculated 
> properties in
> extension. Hope this have any sense and most likely can be improved(code from 
> swift
> sandbox):
> 
> struct DefaultsKey {
> var rawValue : String
> 
> init(_ name: String) {
> rawValue = name
> }
> }
> 
> extension DefaultsKey {
> static var version : DefaultsKey { return 
> DefaultsKey("version") }
> static var code : DefaultsKey { return DefaultsKey("code") }
> }
> 
> func UserDefaults_standard_object(forKey: String) -> Any? {
> switch forKey {
> case "version" : return "1.0.0"
> case "code" : return 12345
> default : return nil
> }
> }
> 
> func Defaults(_ key: DefaultsKey) -> T? {
>return UserDefaults_standard_object(forKey: key.rawValue) as? T
> }
> 
> let version = Defaults(.version)
> let code = Defaults(.code)
> 
> print(version ?? "-no value-", type(of: version)) // 1.0.0 Optional
> print(code ?? "-no value-", type(of: code))   // 12345 Optional
> 
> 
> 
> >
> > public struct DefaultsKey: RawRepresentable, Equatable, Hashable, 
> > Comparable {
> >
> >public var rawValue: String
> >public var hashValue: Int { return rawValue.hash }
> >
> >public init(_ rawValue: String) { self.rawValue = rawValue }
> >public init(rawValue: String) { self.rawValue = rawValue }
> >
> >/* Protocols implementation .. */
> > }
> >
> > Now I can make extensions like:
> >
> > extension DefaultsKey {
> >static let version = DefaultsKey("version »)
> > }
> >
> > And use it to query the UserDefaults.
> >
> > public func Defaults(_ key: DefaultsKey) -> T? {
> >return UserDefaults.standard.object(forKey: key.rawValue) as? T
> > }
> >
> > let version: String? = Defaults(.version)
> >
> > Nice, concise, I love it…
> >
> > But It could be even better to let the compiler check the return type of 
> > the UserDefault for the DefaultKey that I ask if only I could create the 
> > key and bind it to a type. So I tried this:
> >
> > public struct DefaultsKey: RawRepresentable, Equatable, Hashable, 
> > Comparable {
> > …
> > }
> >
> > extension DefaultsKey {
> >static let version = DefaultsKey("version »)
> > }
> >
> > But this doesn’t compile:
> > error: static stored properties not supported in generic types
> >
> > I guess I could keep all the keys outside an extension scope but then it 
> > would not be as concise as with Notification.Name
> >
> > Please let me know if there is indeed a generic way to solve this. Any help 
> > would be greatly appreciated.
> > Thanks in advance.
> >
> > Thierry.
> > ___
> > swift-users mailing list
> > swift-users@swift.org 
> > https://lists.swift.org/mailman/listinfo/swift-users 
> > 
> >
> ___
> swift-users 

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 of it is roughly like this (although the article uses subscript,
which does not allow for a generic implementation so I use a get/set
approach here for brevity):

```
class DefaultsKeys {}
final class DefaultsKey: DefaultsKeys {
let value: String

init(_ value: String) {
self.value = value
}
}

extension UserDefaults {
func get(_ key: DefaultsKey) -> T? {
return object(forKey: key.value) as? T
}

func set(_ key: DefaultsKey, to value: T) {
set(value, forKey: key.value)
}
}

extension DefaultsKeys {
static let version = DefaultsKey("version")
}

let defaults = UserDefaults.standard
defaults.set(.version, to: "1.0")
let version = defaults.get(.version)
print(version ?? "N/A")
```

On Fri, 7 Jul 2017 at 14:00 Vladimir.S via swift-users <
swift-users@swift.org> wrote:

> 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 implement, is with calculated
> properties in
> extension. Hope this have any sense and most likely can be improved(code
> from swift
> sandbox):
>
> struct DefaultsKey {
> var rawValue : String
>
> init(_ name: String) {
> rawValue = name
> }
> }
>
> extension DefaultsKey {
> static var version : DefaultsKey { return
> DefaultsKey("version") }
> static var code : DefaultsKey { return
> DefaultsKey("code") }
> }
>
> func UserDefaults_standard_object(forKey: String) -> Any? {
> switch forKey {
> case "version" : return "1.0.0"
> case "code" : return 12345
> default : return nil
> }
> }
>
> func Defaults(_ key: DefaultsKey) -> T? {
>return UserDefaults_standard_object(forKey: key.rawValue) as? T
> }
>
> let version = Defaults(.version)
> let code = Defaults(.code)
>
> print(version ?? "-no value-", type(of: version)) // 1.0.0 Optional
> print(code ?? "-no value-", type(of: code))   // 12345 Optional
>
>
>
> >
> > public struct DefaultsKey: RawRepresentable, Equatable, Hashable,
> Comparable {
> >
> >public var rawValue: String
> >public var hashValue: Int { return rawValue.hash }
> >
> >public init(_ rawValue: String) { self.rawValue = rawValue }
> >public init(rawValue: String) { self.rawValue = rawValue }
> >
> >/* Protocols implementation .. */
> > }
> >
> > Now I can make extensions like:
> >
> > extension DefaultsKey {
> >static let version = DefaultsKey("version »)
> > }
> >
> > And use it to query the UserDefaults.
> >
> > public func Defaults(_ key: DefaultsKey) -> T? {
> >return UserDefaults.standard.object(forKey: key.rawValue) as? T
> > }
> >
> > let version: String? = Defaults(.version)
> >
> > Nice, concise, I love it…
> >
> > But It could be even better to let the compiler check the return type of
> the UserDefault for the DefaultKey that I ask if only I could create the
> key and bind it to a type. So I tried this:
> >
> > public struct DefaultsKey: RawRepresentable, Equatable, Hashable,
> Comparable {
> > …
> > }
> >
> > extension DefaultsKey {
> >static let version = DefaultsKey("version »)
> > }
> >
> > But this doesn’t compile:
> > error: static stored properties not supported in generic types
> >
> > I guess I could keep all the keys outside an extension scope but then it
> would not be as concise as with Notification.Name
> >
> > Please let me know if there is indeed a generic way to solve this. Any
> help would be greatly appreciated.
> > Thanks in advance.
> >
> > Thierry.
> > ___
> > 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] 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 implement, is with calculated properties in 
extension. Hope this have any sense and most likely can be improved(code from swift 
sandbox):


struct DefaultsKey {
var rawValue : String

init(_ name: String) {
rawValue = name
}
}

extension DefaultsKey {
static var version : DefaultsKey { return 
DefaultsKey("version") }
static var code : DefaultsKey { return DefaultsKey("code") }
}

func UserDefaults_standard_object(forKey: String) -> Any? {
switch forKey {
case "version" : return "1.0.0"
case "code" : return 12345
default : return nil
}
}

func Defaults(_ key: DefaultsKey) -> T? {
  return UserDefaults_standard_object(forKey: key.rawValue) as? T
}

let version = Defaults(.version)
let code = Defaults(.code)

print(version ?? "-no value-", type(of: version)) // 1.0.0 Optional
print(code ?? "-no value-", type(of: code))   // 12345 Optional





public struct DefaultsKey: RawRepresentable, Equatable, Hashable, Comparable {
   
   public var rawValue: String

   public var hashValue: Int { return rawValue.hash }
   
   public init(_ rawValue: String) { self.rawValue = rawValue }

   public init(rawValue: String) { self.rawValue = rawValue }
   
   /* Protocols implementation .. */

}

Now I can make extensions like:

extension DefaultsKey {
   static let version = DefaultsKey("version »)
}

And use it to query the UserDefaults.

public func Defaults(_ key: DefaultsKey) -> T? {
   return UserDefaults.standard.object(forKey: key.rawValue) as? T
}

let version: String? = Defaults(.version)

Nice, concise, I love it…

But It could be even better to let the compiler check the return type of the 
UserDefault for the DefaultKey that I ask if only I could create the key and 
bind it to a type. So I tried this:

public struct DefaultsKey: RawRepresentable, Equatable, Hashable, Comparable 
{
…
}

extension DefaultsKey {
   static let version = DefaultsKey("version »)
}

But this doesn’t compile:
error: static stored properties not supported in generic types

I guess I could keep all the keys outside an extension scope but then it would 
not be as concise as with Notification.Name

Please let me know if there is indeed a generic way to solve this. Any help 
would be greatly appreciated.
Thanks in advance.

Thierry.
___
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] 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 dictionary set 
from a tuple of an integer or string or two, and not being able to do so is 
just plain annoying.


Here what I can find regarding your question:
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170313/033858.html

--- quote ---
...
> Btw, what is your opinion, can we expect (at some point of time) 
Equatable for tuples?
> It is very handy to have Dictionary, type3> in C# and sad that 
we can't have [(type1,type2):type3] in Swift without declaration of new temporary 
struct type and implement Equatable for it.


It would be reasonable, IMO, even before we support the full generalization of 
extensions on non-nominal types, to make tuples of Equatable/Hashable/Comparable 
elements conform to the same protocols by fiat.


-Joe
--- quote ---

So... it seems like we all (including core team) want this, but this will not be 
implemented in Swift4, as I understand.




___
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] 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: String
  public var hashValue: Int { return rawValue.hash }
  
  public init(_ rawValue: String) { self.rawValue = rawValue }
  public init(rawValue: String) { self.rawValue = rawValue }
  
  /* Protocols implementation .. */  
}

Now I can make extensions like:

extension DefaultsKey {
  static let version = DefaultsKey("version »)
}

And use it to query the UserDefaults.

public func Defaults(_ key: DefaultsKey) -> T? {
  return UserDefaults.standard.object(forKey: key.rawValue) as? T
}

let version: String? = Defaults(.version)

Nice, concise, I love it… 

But It could be even better to let the compiler check the return type of the 
UserDefault for the DefaultKey that I ask if only I could create the key and 
bind it to a type. So I tried this:

public struct DefaultsKey: RawRepresentable, Equatable, Hashable, Comparable 
{
…
}

extension DefaultsKey {
  static let version = DefaultsKey("version »)
}

But this doesn’t compile: 
   error: static stored properties not supported in generic types

I guess I could keep all the keys outside an extension scope but then it would 
not be as concise as with Notification.Name

Please let me know if there is indeed a generic way to solve this. Any help 
would be greatly appreciated. 
Thanks in advance.

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


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 in this universe, at least not 
for Swift 4. 

-- 
Adrian Zubarev
Sent with Airmail 

Am 7. Juli 2017 um 04:40:55, David Baraff via swift-users 
(swift-users@swift.org(mailto:swift-users@swift.org)) schrieb:

> 
> 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 dictionary set 
> from a tuple of an integer or string or two, and not being able to do so is 
> just plain annoying.
> 
> ___
> 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] 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) {
return (v, v, v)
}
}

(I'm guessing but am not sure that I've run into (yet) a(nother) situation
which would require higher kinded types?)
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[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 (swiftlang-900.0.45.6 clang-900.0.26)

Target: x86_64-apple-macosx10.9



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