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

2017-07-10 Thread Zhao Xin via swift-users
Thanks.

Zhao Xin

On Tue, Jul 11, 2017 at 2:29 AM, Jordan Rose  wrote:

>
>
> On Jul 7, 2017, at 22:50, Marco S Hyman via swift-users <
> swift-users@swift.org> wrote:
>
>
> 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.
>
>
> The goal is that once the initializer is completed all accesses will go
> through the setter and therefore trigger willSet/didSet behavior. Since a
> local function can be assigned to a property or something and get called
> later, it has to go through the setter as well. So the rules only apply to
> what's directly in the body of the initializer, not anything nested. (This
> includes closures, even.) It might be worth a bug against us at Apple to
> make this more explicit in the documentation, https://bugreport.apple.com.
>
> We also have a bug where 'defer' can trigger willSet and didSet behavior
> as well, SR-1437 . But that really
> is just a bug.
>
> Jordan
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Exceptional values in the Comparable protocol

2017-07-10 Thread Dave Abrahams via swift-users

on Sun Jul 09 2017, Martin R  wrote:

> The Comparable protocol requires that < and == impose a strict total
> order: exactly one of a==b, ab must hold for all values a and b
> of a conforming type.
>
> But it is also noted that a conforming type may contain a subset of
> „exceptional values“ which do not take part in the strict total order
> (such as FloatingPoint.nan).
>
> What does that mean for functions taking comparable arguments, e.g.
>
> func mySuperSort(a: inout [T]) { }
>
> Can the function implementation assume that all values passed to it
> take part in the strict total order? In other words: „exceptional
> values“ must not be passed to the function?

Yes

> Or must the function take that case into account and must not assume
> that exactly one of a==b, a a>b holds for any arguments passed to it?

It need not, but it may do so as a matter of QOI (Quality Of
Implementation).  It is a good idea to make such a function work for NaN
if you can figure out what the semantics should be and it doesn't overly
impact the performance of other important use cases.

Hope this helps,

-- 
-Dave

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


> On Jul 7, 2017, at 22:50, Marco S Hyman via swift-users 
>  wrote:
> 
> 
>> 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.

The goal is that once the initializer is completed all accesses will go through 
the setter and therefore trigger willSet/didSet behavior. Since a local 
function can be assigned to a property or something and get called later, it 
has to go through the setter as well. So the rules only apply to what's 
directly in the body of the initializer, not anything nested. (This includes 
closures, even.) It might be worth a bug against us at Apple to make this more 
explicit in the documentation, https://bugreport.apple.com 
.

We also have a bug where 'defer' can trigger willSet and didSet behavior as 
well, SR-1437 . But that really is just 
a bug.

Jordan

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


[swift-users] Error: Type 'Self.T' does not conform to protocol 'Equatable'

2017-07-10 Thread Muhammad Tahir Vali via swift-users
hey all,

just trying to figuring out a few workarounds for a problem I'm having. Im
having 2 errors both because of associatedtype.

*#1 can be found on the "Vertexable" protocol. This is a strange error
which should work but I'm guessing associatedtypes are causing trouble.
  *
*//error: Type 'Self.T' does not conform to protocol Equatable*

*#2 is me trying to create an array of "Vertexable" objects that can have
different data types since there are use-cases of graphs with different
data types. I can do exactly this when I remove the associatedtype "T" but
I would need to remove  EZNode as well and be forced to manually add it
in for the "EZVertex" class.  *

Error is shown at bottom of this message. You can also refer to
https://www.natashatherobot.com/swift-type-erasure/

for reference and try to create an array of "Pokemon"

Thank you in advance. Sorry if this was too bulky. Im just looking for
conceptual answers to workaround this or why compiler can't accept this.
My last option is to plainly remove both protocols but I find it quite
ironic if I would have to considering the philosophy of protocols.

*//Protocol to create a node*

*protocol** Nodeable : *Equatable*  {*

*associatedtype** T*

*var** data : T { **get* *set** }*

*}*


*//Value-based node*

*public* *struct** EZNode : **Nodeable** {*

*.*

*.*


*typealias** T = **Element*



*var** data : **T*



*init**(data: **T**) {*

*self**.**data** = data*

*}*



*}*

*//Protocol to create a vertex*

*protocol** Vertexable : Hashable {*

*.*

*.*

*associatedtype** T*


*var** node: *EZNode* { **get* *set** }*

*//error: Type 'Self.T' does not conform to protocol Equatable*


*func** getData() -> T*



*}*


*//Reference-based vertex*

*class** EZVertex : **Vertexable* *where* *Element**:
*Equatable* & *Hashable* {*



 *.*

*.*

*typealias** T = **Element*

*var** node: **EZNode**<**T**>*

*//alternative that requires manual entry since it doesnt belong to
"Vertexable" - var node: EZNod *



*var** hashValue: *Int* {*

*return* *"**\**(**self**.**index**) **\**(**self**.**node**)"**.*
hashValue

*}*



*init**(element: **Element**) {*

*self**.**node** = **EZNode**(data: element)*

*self**.**index** = index*

*}*



*static* *func** ==(lhs: **EZVertex**<**Element**>, rhs: **EZVertex**<*
*Element**>) -> *Bool* {*

*return** lhs.**node**.**data* ==* rhs.**node**.**data* &&* lhs.*
*index** == rhs.**index*

*}*



*func** getData() -> **Element** {*

*return* *node**.**data*

*}*



*}*


*let** node1 = **EZNode**(data: **1**)*

*let** node2 = **EZNode**(data: **10.0**)*

*let** vertex1 = **EZVertex**(element: **node1**.**data**, index: **1**)*

*let** vertex2 = **EZVertex**(element: **node2**.**data**, index: **2**)*


 *//protocol "Vertexable" can only be used a generic constraint because it
has Self or associatedtype requirements. *

*let** arr1 : [**Vertexable**] = [**vertex1**,**vertex2**]*


-- 
Best Regards,

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


Re: [swift-users] komihåg swift 4 Vector

2017-07-10 Thread Jens Persson via swift-users
Oops sorry, I intended to send that code to myself but autocompletion made
it go to swift-users ...
/Jens

On Mon, Jul 10, 2017 at 7:46 PM, Jens Persson via swift-users <
swift-users@swift.org> wrote:

> protocol VectorStorage {
> associatedtype A
> associatedtype B
> associatedtype C
> associatedtype D
> var elements: (A, B, C, D) { get set }
> init(elements: (A, B, C, D))
> }
> struct VectorStorage2 : VectorStorage {
> var elements: (T, T, Void, Void)
> }
> struct VectorStorage3 : VectorStorage {
> var elements: (T, T, T, Void)
> }
>
> protocol VectorIndex {
> associatedtype StorageOf8BitElements: VectorStorage
> associatedtype StorageOf16BitElements: VectorStorage
> associatedtype StorageOf32BitElements: VectorStorage
> associatedtype StorageOf64BitElements: VectorStorage
> static func vectorStorage(for: V.Type, from fn: (Self) ->
> V.Element) -> V.Storage
> where V.Index == Self
> func getElement(from: V) -> V.Element where V.Index == Self
> func setElement(of vector: inout V, to value: V.Element)
> where V.Index == Self
> }
> enum Index2 : VectorIndex {
> typealias StorageOf8BitElements = VectorStorage2
> typealias StorageOf16BitElements = VectorStorage2
> typealias StorageOf32BitElements = VectorStorage2
> typealias StorageOf64BitElements = VectorStorage2
> case i0, i1
> static func vectorStorage(for: V.Type, from fn: (Index2) ->
> V.Element) -> V.Storage
> where V.Index == Index2
> {
> let storage = V.Storage.init(elements: (
> unsafeBitCast(fn(.i0), to: V.Storage.A.self),
> unsafeBitCast(fn(.i1), to: V.Storage.B.self),
> () as! V.Storage.C,
> () as! V.Storage.D
> ))
> return storage
> }
> func getElement(from vector: V) -> V.Element where V.Index
> == Index2 {
> switch self {
> case .i0: return unsafeBitCast(vector.storage.elements.0, to:
> V.Element.self)
> case .i1: return unsafeBitCast(vector.storage.elements.1, to:
> V.Element.self)
> }
> }
> func setElement(of vector: inout V, to value: V.Element)
> where V.Index == Index2 {
> switch self {
> case .i0: vector.storage.elements.0 = unsafeBitCast(value, to:
> V.Storage.A.self)
> case .i1: vector.storage.elements.1 = unsafeBitCast(value, to:
> V.Storage.B.self)
> }
> }
> }
> enum Index3 : VectorIndex {
> typealias StorageOf8BitElements = VectorStorage3
> typealias StorageOf16BitElements = VectorStorage3
> typealias StorageOf32BitElements = VectorStorage3
> typealias StorageOf64BitElements = VectorStorage3
> case i0, i1, i2
> static func vectorStorage(for: V.Type, from fn: (Index3) ->
> V.Element) -> V.Storage
> where V.Index == Index3
> {
> let storage = V.Storage.init(elements: (
> unsafeBitCast(fn(.i0), to: V.Storage.A.self),
> unsafeBitCast(fn(.i1), to: V.Storage.B.self),
> unsafeBitCast(fn(.i2), to: V.Storage.C.self),
> () as! V.Storage.D
> ))
> return storage
> }
> func getElement(from vector: V) -> V.Element where V.Index
> == Index3 {
> switch self {
> case .i0: return unsafeBitCast(vector.storage.elements.0, to:
> V.Element.self)
> case .i1: return unsafeBitCast(vector.storage.elements.1, to:
> V.Element.self)
> case .i2: return unsafeBitCast(vector.storage.elements.2, to:
> V.Element.self)
> }
> }
> func setElement(of vector: inout V, to value: V.Element)
> where V.Index == Index3 {
> switch self {
> case .i0: vector.storage.elements.0 = unsafeBitCast(value, to:
> V.Storage.A.self)
> case .i1: vector.storage.elements.1 = unsafeBitCast(value, to:
> V.Storage.B.self)
> case .i2: vector.storage.elements.2 = unsafeBitCast(value, to:
> V.Storage.C.self)
> }
> }
> }
> protocol Vector {
> associatedtype Index: VectorIndex
> associatedtype Element
> associatedtype Storage: VectorStorage
> var storage: Storage { get set }
> init(elementForIndex: (Index) -> Element)
> }
> extension Vector {
> subscript(index: Index) -> Element {
> get { return index.getElement(from: self) }
> set { index.setElement(of: , to: newValue) }
> }
> func map(transform: (Element) -> NewElement) ->
> SV32 {
> return SV32 { index in transform(self[index]) }
> }
> func map(transform: (Element) -> NewElement) ->
> SV64 {
> return SV64 { index in transform(self[index]) }
> }
> }
>
> protocol VectorElement {
> associatedtype BitPattern
> }
>
>
> extension UInt8 : VectorElement { typealias BitPattern = UInt8 }
> extension UInt16 : VectorElement { typealias BitPattern = UInt16 }
> extension UInt32 : VectorElement { typealias BitPattern = UInt32 }
> extension UInt64 : 

Re: [swift-users] How to write better Swift

2017-07-10 Thread Gerriet M. Denkmann via swift-users

> On 10 Jul 2017, at 22:35, Geordie J via swift-users  
> wrote:
> 
> Would “didSet" on a normal variable work for you?

willSet/didSet usually is a good solution (which I had forgotten about).

But in my case it seems not to help - see the corrected code below

> var status: Int {
>  didSet { doSomething() }
> }
> 
> Geordie
> 
> 
>> Am 10.07.2017 um 17:34 schrieb Gerriet M. Denkmann via swift-users 
>> :
>> 
>> This works (Xcode Version 8.3.2 (8E2002)):
>> 
>> class SomeClass
>> {
>>  private var privateStatus: Int  
>> 
>>  var status: Int
>>  {
>>  get{ return privateStatus }
>>  set(new)
>>  {
>>  if new == privateStatus {return}

oldDerived = derivedStatus // depends on status
privateStatus = new
newDerived = derivedStatus 

if newDerived != oldDerived … do something … 

… do something more here …
>>  }
>>  }
>> }
>> 
>> But is this “privateStatus” really necessary?
>> If not, how can it be avoided?


Quincey had an excellent idea: one should be able to write:

var status: Int
{
var privateStatus: Int  //  makes it much clearer that 
privateStatus really only belongs to status

get{ return privateStatus }
set(new)
{
…
privateStatus = new
}
}

Gerriet.

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


Re: [swift-users] How to write better Swift

2017-07-10 Thread Adrian Zubarev via swift-users
It should be willSet because in the example the job is executed before the new 
value is set.

class SomeClass {

var status: Int {
willSet {
guard newValue != self.status { return }
// do something here
}
}
}


-- 
Adrian Zubarev
Sent with Airmail

Am 10. Juli 2017 um 17:35:34, Geordie J via swift-users (swift-users@swift.org) 
schrieb:

Would "didSet" on a normal variable work for you?

var status: Int {
didSet { doSomething() }
}

Geordie


> Am 10.07.2017 um 17:34 schrieb Gerriet M. Denkmann via swift-users 
> :
>  
> This works (Xcode Version 8.3.2 (8E2002)):
>  
> class SomeClass
> {
> private var privateStatus: Int
>  
> var status: Int
> {
> get{ return privateStatus }
> set(new)
> {
> if new == privateStatus {return}
>  
> … do something here …
>  
> privateStatus = new
> }
> }
> }
>  
> But is this “privateStatus” really necessary?
> If not, how can it be avoided?
>  
> Gerriet.
>  
> ___
> 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] How to write better Swift

2017-07-10 Thread Geordie J via swift-users
Would "didSet" on a normal variable work for you?

var status: Int {
  didSet { doSomething() }
}

Geordie


> Am 10.07.2017 um 17:34 schrieb Gerriet M. Denkmann via swift-users 
> :
> 
> This works (Xcode Version 8.3.2 (8E2002)):
> 
> class SomeClass
> {
>   private var privateStatus: Int  
> 
>   var status: Int
>   {
>   get{ return privateStatus }
>   set(new)
>   {
>   if new == privateStatus {return}
>   
>   … do something here …
> 
>   privateStatus = new
>   }
>   }
> }
> 
> But is this “privateStatus” really necessary?
> If not, how can it be avoided?
> 
> Gerriet.
> 
> ___
> 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 write better Swift

2017-07-10 Thread Gerriet M. Denkmann via swift-users
This works (Xcode Version 8.3.2 (8E2002)):

class SomeClass
{
private var privateStatus: Int  

var status: Int
{
get{ return privateStatus }
set(new)
{
if new == privateStatus {return}

… do something here …

privateStatus = new
}
}
}

But is this “privateStatus” really necessary?
If not, how can it be avoided?

Gerriet.

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