[swift-users] Reducing the boilerplate for my ID types

2016-05-17 Thread Ray Fix via swift-users
Hello, I have some ID types that are simple ints coming back from a database. I wanted to improve type safety so I don’t accidentally assign product IDs to user IDs, etc. I want to be able to print it and use it as a dictionary key. So it is a trivial wrapper of Int. E.g. struct CustomerID

Re: [swift-users] Reducing the boilerplate for my ID types

2016-05-17 Thread Ray Fix via swift-users
> On May 17, 2016, at 7:24 PM, Brent Royal-Gordon > wrote: > >> I have some ID types that are simple ints coming back from a database. I >> wanted to improve type safety so I don’t accidentally assign product IDs to >> user IDs, etc. I want to be able to print it and use it as a dictionary

Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Ray Fix via swift-users
> On May 18, 2016, at 3:56 AM, Artyom Goncharov via swift-users > wrote: > > var noOptDict = ["one": 1, "two": 2, "three": 3 ] > noOptDict["one"] = nil Wow, interesting. To me this was surprising behavior too. The comment for Dictionary subscript says: /// Access the value associated w

[swift-users] access violation when weak variable

2016-07-09 Thread Ray Fix via swift-users
Hi! When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a access violation. I think this is a bug, but want to make sure I am not missing something. Best regards, Ray //: Playground - noun: a place where people can play import UIKit class Person: CustomStringConvertible {

[swift-users] generic function called recursively not compiling

2016-08-04 Thread Ray Fix via swift-users
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 = inpu

[swift-users] remove "Example" from multiline playground markup

2016-08-29 Thread Ray Fix via swift-users
With Xcode playground markup, if I write a multiline Swift code example such as: for item in collection { print(item) } The rendered markup it always has the header “Example” Is there any way to write multiline code, or modify a stylesheet so that the heading “Example” is supress

Re: [swift-users] remove "Example" from multiline playground markup

2016-08-31 Thread Ray Fix via swift-users
f step with commonmark 0.25, which allows > you to use a subset of raw HTML including and code > > -- E > > >> On Aug 29, 2016, at 3:07 AM, Ray Fix via swift-users >> wrote: >> >> With Xcode playground markup, if I write a multiline Swift code example s

[swift-users] tuples and metatypes

2016-08-31 Thread Ray Fix via swift-users
Hi. Asking for a friend ;] Tuples cannot conform to protocols or be extended because they are compound types. Is it correct to say that this is true because there is not a unique metatype for compound types? IOW, there is no unique existential container thingy for a tuples? (Not sure if I am

Re: [swift-users] How to merge two dictionaries?

2016-11-11 Thread Ray Fix via swift-users
Hi Mr Bee, The reason I don’t think it is provided is because it is difficult to know what to do when keys collide. You could easily write such a thing and decide your own policy. For example: let d1 = ["Apples": 20, "Oranges": 13] let d2 = ["Oranges": 3, "Cherries": 9] extension Dictionary

[swift-users] Getting at the bytes of Data

2016-12-09 Thread Ray Fix via swift-users
I am writing a tutorial and creating a wrapper on streaming compression. In the code, I setup the stream source pointer: (input is of type Data) // set up the stream with the input source input.withUnsafeBytes { (bytes: UnsafePointer) in stream.src_ptr = bytes } stream.src_size = in

Re: [swift-users] Getting at the bytes of Data

2016-12-09 Thread Ray Fix via swift-users
Hi Philippe, Thank you for your response! > On Dec 9, 2016, at 8:44 AM, Philippe Hausler wrote: > > So letting the bytes escape from the closure could potentially be in that > territory of “bad things may happen”. If you know for certain that the Data > will not be mutated and it’s underlying

Re: [swift-users] Getting at the bytes of Data

2016-12-09 Thread Ray Fix via swift-users
Keeping the closure open until the method ends and returning the result was eye opening. Thanks again! I thought about making the destination buffer a Data as well but I kind of feel like it is better the way it is as it keeps down the number of scopes. It kind of starts feeling like a pyramid

[swift-users] unsafe memory model questions

2016-12-11 Thread Ray Fix via swift-users
Hello, So bindMemory is part of the memory model and memory can only bind to one type at a time to avoid aliasing. But what does binding actually do? Is it somehow communicating with the optimizer? A tangentially related second question, in the following example: let count = 3 let mutableP

Re: [swift-users] Memory Leak Indicated when Derived from NSObject

2016-12-16 Thread Ray Fix via swift-users
FWIW, seeing this too. Also, when I boiled the project down to a macOS command line and run the “leaks" cli I don’t see the leak. 🤔 Ray > On Oct 14, 2016, at 9:42 AM, Chris Chirogene via swift-users > wrote: > > Xcode8 is showing a memory leak in instruments and the memory graph. I have >

Re: [swift-users] Memory Leak Indicated when Derived from NSObject

2016-12-19 Thread Ray Fix via swift-users
Thanks for the update Chris. Hmm... So, I get memory runtime issues if I run this on an actual device iPad Air 2 (iOS 10.2) with Version 8.2 (8C38). Can’t get it to happen on the simulator. Can’t get it to happen if I make a macOS command line tool and inspect it with the leaks command. (I r

Re: [swift-users] Using 'SomeProtocol' as a concrete type conforming to protocol 'SomeProtocol' is not supported

2016-12-28 Thread Ray Fix via swift-users
Using Optional, your enum type goes away. (I think it is a great solution unless you need something more than .element and .none in real life.) Great to get all that optional machinery for missing values for free! Then you can constrain elements simply from the Element protocol as in as in:

[swift-users] are large values (not passed through a protocol) ever put on the heap?

2017-01-03 Thread Ray Fix via swift-users
There was a great talk at WWDC 2016 about internals and Swift performance. https://developer.apple.com/videos/play/wwdc2016/416/ At one point, Arnold Schwaighofer says, "Copying of large values incurs heap allocation.” What isn’t clear

[swift-users] operators and external parameters

2017-01-24 Thread Ray Fix via swift-users
When defining an operator such as == you can implicitly specify external parameters: struct Path: Equatable { var value: String static func ==(lhs: Path, rhs: Path) -> Bool { return lhs.value == rhs.value } } Is this considered okay, or is it bad form and it should really b

[swift-users] Avoiding an extra copy withUnsafeBytes

2017-01-26 Thread Ray Fix via swift-users
To compute a hash, I want to loop through bytes of a trivial type. To get the bytes, I use withUnsafeBytes hands me a rawBufferPointer collection type. Great! However this call also requires that the "of arg" be declared as a “var" instead of “let” because it is inout. So to do it genericall

Re: [swift-users] Array elements assign to variables

2017-01-27 Thread Ray Fix via swift-users
How about: typealias PixelTuple = (red: UInt8, green: UInt8, blue: UInt8, brightness: UInt8) let pixelCount = 10 let pointer = UnsafeMutableRawPointer.allocate(bytes: 4*pixelCount, alignedTo: 16) defer { pointer.deallocate(bytes: 4*pixelCount, alignedTo: 16) } let tuplesPointer = pointer.bi

Re: [swift-users] Initializer accessing self

2017-02-01 Thread Ray Fix via swift-users
In the working version, I think the timer is first set to nil and then changed to the scheduled timer. In the non-working case time can only be set exactly once and self access is not legal until after it is set. Ray > On Feb 1, 2017, at 8:06 PM, Brandon Knope via swift-users > wrote: > >

[swift-users] unowned self in closure in a playground

2017-02-21 Thread Ray Fix via swift-users
Hi, The following code crashes: class Demo { var value = 0 lazy var increment: (Int) -> Void = { [unowned self] by in self.value += by print(self.value) } } Demo().increment(3) error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BREAKPOINT (code=EXC

Re: [swift-users] unowned self in closure in a playground

2017-02-21 Thread Ray Fix via swift-users
Thanks. My comment below. > > On Feb 21, 2017, at 11:18 AM, Marco S Hyman wrote: > >> The following code crashes: >> >> class Demo { >> var value = 0 >> lazy var increment: (Int) -> Void = { [unowned self] by in >>self.value += by >>print(self.value) >> } >> } >> >> Demo().incremen

Re: [swift-users] unowned self in closure in a playground

2017-02-21 Thread Ray Fix via swift-users
> On Feb 21, 2017, at 7:32 PM, rintaro ishizaki wrote: > > lifetime guaranteed to be around until the method call completes > > AFAIK, this is true. > However, in this case, it's not a method call. The lifetime is only > guaranteed until getter:increment completes. > > Demo().increment(3) > |

[swift-users] withoutActuallyEscaping example question

2017-03-26 Thread Ray Fix via swift-users
Hi, One of the motivating examples for withoutActuallyEscaping looks like the following. This predictably doesn’t work because "use of non-escaping parameter 'predicate' may allow it to escape" func myFilter(array: [Int], predicate: (Int) -> Bool) -> [Int] { return Array(array.lazy.filter

Re: [swift-users] withoutActuallyEscaping example question

2017-03-26 Thread Ray Fix via swift-users
rgue this is somewhat confusing, but it might be difficult to >> change the overload resolution rules in a way where the first overload is >> always chosen. >> >> Slava >> >>> On Mar 26, 2017, at 12:13 AM, Ray Fix via swift-users >>> mailto:swift-users

[swift-users] Constraining the conforming type of a protocol

2017-07-29 Thread Ray Fix via swift-users
Hi, I had a question about defining protocols. Originally I wrote: protocol Toggling where Self: Equatable { static var all: [Self] { get } func toggled() -> Self mutating func toggle() } extension Toggling { func toggled() -> Self { let current = Self.all.index(of: self) ?? 0 l

Re: [swift-users] Constraining the conforming type of a protocol

2017-07-30 Thread Ray Fix via swift-users
> … > } > > It’s a bug that placing the constraint on ‘Self’ does not have the same > effect though; do you mind filing a JIRA? > > Slava > >> On Jul 29, 2017, at 12:24 PM, Ray Fix via swift-users > <mailto:swift-users@swift.org>> wrote: >>

[swift-users] Decoding dates with dateDecodingStrategy

2017-10-22 Thread Ray Fix via swift-users
I had a question about date deserialization. If a container (or its sub-containers) has dates in multiple formats, is the best way to go about it to decode a string and then run each one through a custom date formatter? It seems strange to me that dateDecodingStrategy is a top-level decoder