[swift-users] Protocols, `mutating` and Value vs Reference Types.

2016-04-29 Thread Hooman Mehr via swift-users
Hi, I am designing APIs that need to support both reference (class/object) and value types. I am running into restrictions of `mutating` keyword in my protocols and this is causing a lot of duplication of code. In order to understand what I mean please take a look at this gist

Re: [swift-users] Protocols, `mutating` and Value vs Reference Types.

2016-04-29 Thread Hooman Mehr via swift-users
Thank you very much for your response. I made the change in the gist . This takes me back to the same error that forced my into duplication path: “Cannot assign through subscript: ‘self’ is immutable” for the object case. The gist

[swift-users] SubSequence Index bug in generic functions?

2016-05-11 Thread Hooman Mehr via swift-users
The reason this is appearing here before filing a radar is getting a quick feedback as I have been assuming this is the expected behavior but Nate Cook told me otherwise. See this thread for the initial discuss

Re: [swift-users] SubSequence Index bug in generic functions?

2016-05-11 Thread Hooman Mehr via swift-users
Filed as: SR-1487 <https://bugs.swift.org/browse/SR-1487> > On May 11, 2016, at 1:51 PM, Dmitri Gribenko wrote: > > On Wed, May 11, 2016 at 1:48 PM, Hooman Mehr via swift-users > wrote: >> The reason this is appearing here before filing a radar is getting a quick &g

Re: [swift-users] Method of same name is shadowing a property in Swift 3

2016-09-13 Thread Hooman Mehr via swift-users
As I see it, this is serious bug. In Swift 3.0, parameter labels (or lack of them) are supposed to be a part of the function name. Otherwise, the two `key` definitions would create a name collision and should have been rejected in the first place. This should not happen, because only `key(at:)`

[swift-users] A sample Rational number type

2016-09-30 Thread Hooman Mehr via swift-users
For anybody who is interested: This gist contains a Rational number implementation for Swift 3.0. It is a single file that you can paste in a playground and take a look, or remove the last few lines and drop the file into your ow

Re: [swift-users] A sample Rational number type

2016-10-02 Thread Hooman Mehr via swift-users
> On Oct 2, 2016, at 5:23 PM, Dave Abrahams via swift-users > wrote: > Presumably you mean: > > let r2 = r±0.0005 // turn a Rational into a Double with no more than >// .0005 rounding error > > ? That is supercute! > It is actually the other way around: It returns a rati

Re: [swift-users] A sample Rational number type

2016-10-05 Thread Hooman Mehr via swift-users
I encountered a bug in the standard library while working on an exact conversion from floating point types to my rational type (when you pass a tolerance of zero) SR-2868 : If the value of t

Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Hooman Mehr via swift-users
I recommend having explicit precondition and reducing repetition like this: import Foundation func random(from range: CountableRange) -> Int { precondition(range.count > 0, "The range can't be empty.") return random(from: CountableClosedRange(range)) } func random(

Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Hooman Mehr via swift-users
> On Oct 12, 2016, at 3:21 AM, Jean-Denis Muys via swift-users > wrote: > > > But this is not very DRY. > > What would be a more idiomatic way? > The more idiomatic way is to look at API design in a new way. Note these points: 1. `Countable` variant is preferred when you want to deal with

Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-13 Thread Hooman Mehr via swift-users
I recommend having explicit precondition and reducing repetition like this: import Foundation func random(from range: CountableRange) -> Int { precondition(range.count > 0, "The range can't be empty.") return random(from: CountableClosedRange(range)) } func random(

Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-13 Thread Hooman Mehr via swift-users
On more thing: The result type of my `random()` extension is optional. This might not be ideal. It is not an expected and likely case that `random()` returns nil. I would personally prefer it to be non-optional and crash if the array is empty, just like it would if you tried to reference a non

Re: [swift-users] Build Android GUI apps with Swift 3.0 via a framework/library

2016-10-13 Thread Hooman Mehr via swift-users
I think it is too soon to tackle this issue. I think there is room for making adapters for a lightweight GUI toolkit for embedded applications. For example, a Swift adapter for EFL (of Enlightenment) or something like that. But when we talk about Androi

Re: [swift-users] Overload by return type optionality?

2016-10-13 Thread Hooman Mehr via swift-users
> On Oct 13, 2016, at 3:31 PM, Rick Mann via swift-users > wrote: > > Would it make sense to be able to specify priority for a set of overloaded > methods to help resolve ambiguity? This might be pretty useful in some situations, but I am not sure if the semantic complexity that it introduce

Re: [swift-users] Subsequences and shared indices

2016-10-13 Thread Hooman Mehr via swift-users
This is a bug reported multiple times in different forms. My version of it is: SR-1487 . It remains open because it is not easy to fix with the existing design of String. Apparently core standard library team are working on an overhaul of String to addres

Re: [swift-users] Subsequences and shared indices

2016-10-13 Thread Hooman Mehr via swift-users
`Slice` family of types (there are many) are well documented to share the indices and inherit the semantics. All collections that have a SubSequence of a Slice type, share indices. Unfortunately, standard library is not well documented in general and collection API have undergone big changes in

Re: [swift-users] Overload by return type optionality?

2016-10-13 Thread Hooman Mehr via swift-users
I know, but a simple let x = 2/3 becomes ambiguous which I don’t like. > On Oct 13, 2016, at 8:00 PM, Mark Lacey wrote: > > >> On Oct 13, 2016, at 5:37 PM, Hooman Mehr via swift-users >> mailto:swift-users@swift.org>> wrote: >> >> >>>

Re: [swift-users] Migrating to Swift 3 Dispatch

2016-10-16 Thread Hooman Mehr via swift-users
> On Oct 16, 2016, at 11:25 AM, Thierry Passeron via swift-users > wrote: > > Hello All, > > I’m in the process of migrating older code to Swift 3 and I’m stuck on this > one. > How do you create a timer dispatch source? > > Old code: > > let source = dispatch_source_create(DISPATCH_SOURCE_

Re: [swift-users] Subsequences and shared indices

2016-10-16 Thread Hooman Mehr via swift-users
Thanks to Ole Begemann (who closed my bug report), I found out that Nate Cook submitted a fix for this issue a few day ago. The fix will hopefully make it to the next maintenance release of Swift. The documentation of range subscript (which is the main

Re: [swift-users] Why can I not filter or map a dictionary to another dictionary?

2016-10-26 Thread Hooman Mehr via swift-users
The rational for returning Array is here: https://github.com/apple/swift/blob/master/docs/StdlibRationales.rst#high-order-functions-on-collections-return-arrays I have this s

Re: [swift-users] Implementing signum

2016-11-20 Thread Hooman Mehr via swift-users
Let me explain this a bit further: Swift generic programming is very different from C++ template programming. Swift compiler needs to type-check generic code on spot, not while instantiating the template (because in Swift, instantiation can occur at runtime if the generic code is from a differe

Re: [swift-users] Extending Arrays of specific type, and specialization

2016-11-21 Thread Hooman Mehr via swift-users
This is not possible in Swift 3.0. Swift 4.0 will improve things with conditional conformances. For now, the best solution is using global functions instead of extending types or protocols. For example you can do this now: extension Array where Element: FloatingPoint { func sum() -> E

Re: [swift-users] Extending Arrays of specific type, and specialization

2016-11-22 Thread Hooman Mehr via swift-users
It is good to know that extension Array where Element == Double { } will work pretty soon with Swift 3.1. Back to reduce(0,+): If we get specialized instance for a reduce(0,+), so that it is known that “+” is a (Double, Double)->Double function, LLVM’s auto-vectorization should be able to o

Re: [swift-users] Any equivalent to Java's AtomicInteger?

2016-11-22 Thread Hooman Mehr via swift-users
Standard library source code already includes internal atomic counters. Introducing these apparently is on the agenda but post Swift 4.0 as part of a general language level support for concurrency. For now, the preferred API for such things is GCD (Grand Central Dispatch) provided through Dispat

Re: [swift-users] Extending Arrays of specific type, and specialization

2016-11-22 Thread Hooman Mehr via swift-users
For example, this reduces the six variants of sum to two: public protocol ContiguousBufferedArray: RandomAccessCollection { func withUnsafeBufferPointer(_ body: (UnsafeBufferPointer) throws -> R) rethrows -> R } extension Array: ContiguousBufferedArray {} extension ContiguousArray: Cont

Re: [swift-users] Extending Arrays of specific type, and specialization

2016-11-22 Thread Hooman Mehr via swift-users
By the way, even without new Swift 3.1 feature, this works, providing optimized sum function for all three types: extension ContiguousBufferedArray where Iterator.Element == Double { func sum() -> Double { var result = Double() withUnsafeBufferPointer { vDSP_sveD

Re: [swift-users] Any equivalent to Java's AtomicInteger?

2016-11-22 Thread Hooman Mehr via swift-users
As you noted, this is extremely inefficient and compiler can’t do anything about it. GCD is meant to be used as a high-level concurrency design tool. If you really need atomic integer, your best bet is writing it in C or Objective-C and calling it in Swift. > On Nov 22, 2016, at 4:48 PM, Rick

Re: [swift-users] How to dispatch on the size of Int?

2016-11-23 Thread Hooman Mehr via swift-users
func foo_impl(value: Int32) { /* ... */ } func foo_impl(value: Int64) { /* ... */ } func foo(value: Int) { #if arch(i386) || arch(arm) foo_impl(value: Int32(value)) #else foo_impl(value: Int64(value)) #endif } > On Nov 23, 2016, at 1:31 PM, Martin R via swift-users

Re: [swift-users] How to dispatch on the size of Int?

2016-11-23 Thread Hooman Mehr via swift-users
I agree, there should be a platform condition that just indicates native word size, such as arch(32bit) and arch(64bit). > On Nov 23, 2016, at 2:18 PM, Martin R wrote: > > Yes, I had forgotten about that, thank you! That would satisfy all criteria. > And with > >func foo(value: Int) { >

Re: [swift-users] CNLabeledValue var

2017-01-09 Thread Hooman Mehr via swift-users
Ouch! This is a side effect of ObjC lightweight generics being imported as Swift generics in Swift 3.0. Since there is no common superclass that satisfies NSCopying & NSSecureCoding and the fact that NSSecureCoding has static requirements means you can’t do it (at least without hacks such as e

Re: [swift-users] Generic initializer's parameter type not being resolved

2017-01-31 Thread Hooman Mehr via swift-users
Update your where clause to: where Type: PluginDataProtocol, Type.Parent == Monster { > On Jan 31, 2017, at 10:36 AM, Andrey Fidrya via swift-users > wrote: > > Hi All, > > I've encountered a very strange problem where generic initializer's parameter > type isn't resolved correctly

Re: [swift-users] Using withUnsafePointer on char arrays within C structs

2017-03-01 Thread Hooman Mehr via swift-users
Your co-worker needs to get passed the learning curve of these “unsafe” APIs and note that Swift arrays are complex data structures. &allZeros does not give you a pointer to a bunch of zero bytes, but a pointer to a struct that contains the private implementation details of allZeros array. Her

Re: [swift-users] Using withUnsafePointer on char arrays within C structs

2017-03-02 Thread Hooman Mehr via swift-users
Yes, the easiest way is to rely on compiler magic for ObjC/C interoperability, but it is also important to know what is really happening. My preferred version of the compiler magic is this actually: func allZerosUUID() -> String { return NSUUID(uuidBytes: [UInt8](repeating: 0, count: 32)).u

Re: [swift-users] Set with element of NSObject subclasses didn't work as expected

2017-03-29 Thread Hooman Mehr via swift-users
Yes, this is a serious shortcoming in the transition to Swift as the main application programming language for Apple platforms. With the existing architecture (lack of stable Swift ABI) system frameworks and any binary distributed frameworks still have to be written in Objective-C. If you are a

Re: [swift-users] Set with element of NSObject subclasses didn't work as expected

2017-03-29 Thread Hooman Mehr via swift-users
> On Mar 29, 2017, at 11:36 AM, Joe Groff via swift-users > wrote: > > Perhaps the NSObject implementation of `hashValue` should be final to help > with this. Good idea. And as I mentioned in my other reply, we need further emphasis and clarification in the documentation who are starting Ap

Re: [swift-users] Set with element of NSObject subclasses didn't work as expected

2017-03-29 Thread Hooman Mehr via swift-users
I dropped two words in my initial sentence. The complete sentence is: We need further emphasis and clarification in the documentation for people who are starting Apple development with Swift with no prior knowledge of ObjC/NSObject stuff. > On Mar 29, 2017, at 11:40 AM, Hooman Mehr via sw

Re: [swift-users] Why does withUnsafePointer(to:) require a var argument?

2017-04-27 Thread Hooman Mehr via swift-users
You should be able to type your `dataBuffer ` as [Int8] (Byte array). Then you won’t need `withUnsafeMutableBytes`. You can simply call it like this: self.request = c_library_call(¶ms, dataBuffer) // Call as if it is a C array It works because of C interoperability compiler magic. As long as th

Re: [swift-users] Why does withUnsafePointer(to:) require a var argument?

2017-04-27 Thread Hooman Mehr via swift-users
> On Apr 27, 2017, at 7:31 PM, Rick Mann wrote: > > >> On Apr 27, 2017, at 18:56 , Hooman Mehr wrote: >> >> You should be able to type your `dataBuffer ` as [Int8] (Byte array). Then >> you won’t need `withUnsafeMutableBytes`. You can simply call it like this: >> >> self.request = c_library

Re: [swift-users] What is up with names not being Strings any more in Swift 4?

2017-07-18 Thread Hooman Mehr via swift-users
I think this should be a feature of Xcode to automatically generate / maintain these constants: When you add assets or create interface builder files, a plug-in could take care of generating / updating such constants and you would get auto-complete and type safety for free. > On Jul 18, 2017, a

Re: [swift-users] Lifetime of objects in `class` methods

2017-07-24 Thread Hooman Mehr via swift-users
In the class method case, the scope of your `manager` object is local the the class method and it is deallocated immediately as function returns (before the closure gets a chance to be called). > On Jul 24, 2017, at 12:22 PM, Nate Birkholz via swift-users > wrote: > > If I use these two clas

Re: [swift-users] Extending 2D arrays of genetic type

2017-07-30 Thread Hooman Mehr via swift-users
You can do something like this: extension Collection where Element: Collection, Index == Element.Index { subscript(i: Index, j: Index) -> Element.Element { get { return self[i][j] } } } extension MutableCollection where Element: MutableCollection, Index == Element.Index {

Re: [swift-users] Simultaneous accesses, but modification requires exclusive access

2017-07-30 Thread Hooman Mehr via swift-users
> On Jul 24, 2017, at 2:38 AM, somu subscribe via swift-users > wrote: > > Thank a lot Quinn, your solution to use inout works well without crashing. > > Question 1: > - Also changing Helper to a class doesn’t seem to crash. Is that a solution > that wouldn’t cause a crash or just works by ch

Re: [swift-users] Can you use @autoclosure in a setter?

2017-09-11 Thread Hooman Mehr via swift-users
But the expression that is assigned to the property will be eagerly evaluated to produce `newValue`. So this will not accomplish what Nevin is trying to do. > On Sep 11, 2017, at 3:08 PM, Slava Pestov via swift-users > wrote: > > You can have valueSource store a closure that captures the auto

Re: [swift-users] dynamic requires @objc in Framework, but not in monolithic project

2017-09-13 Thread Hooman Mehr via swift-users
When you create a new project in Xcode 9, the defaults for compiler settings are different. The errors you see are the result of running in full (strict) Swift 4.0 mode which has some such breaking changes. > On Sep 13, 2017, at 3:56 PM, Rick Mann via swift-users > wrote: > > I have an iOS ap

Re: [swift-users] dynamic requires @objc in Framework, but not in monolithic project

2017-09-13 Thread Hooman Mehr via swift-users
Documentation is behind the actual state. What you see is the result of this change: SE-0160 Limiting @objc inference > On Sep 13, 2017, at 5:18 PM, Roderick Mann wrote: > > Ah. That's the difference. Okay.

Re: [swift-users] Generics question - can you handle both optional and non-optional args with the same function?

2018-01-11 Thread Hooman Mehr via swift-users
Hi, Two points: 1) What you want to do is a common operation in functional programming called flatMap. Optional type already supports it. To get “x != nil ? f(x) : nil” you say: x.flatMap(f) 2) You don’t need multiple versions, because there is a subtype-supertype relationship between func

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

2016-03-25 Thread Hooman Mehr via swift-users
> On Mar 25, 2016, at 2:51 PM, Jason Sadler via swift-users > wrote: > > (My particular use case can be seen here: > https://gist.github.com/sadlerjw/2cc16b4375b02fe7f400 > … and the best > information I’ve been able to find on this so

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

2016-03-25 Thread Hooman Mehr via swift-users
You can also get a heterogenous dictionary, but it needs a type erasing helper box. With the following declarations: public func ==(lhs: T, rhs: Any) -> Bool { if let rhs = rhs as? T { return lhs == rhs } else { return false } } public struct AnyKey : Hashable { public let hash

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

2016-03-25 Thread Hooman Mehr via swift-users
lue = key.hashValue self.key = key self.equals = { $0 as! T == $1.key } } } public func == (lhs: AnyKey, rhs: AnyKey) -> Bool { return lhs.equals(lhs,rhs) } > On Mar 25, 2016, at 5:59 PM, Hooman Mehr via swift-users > wrote: > > You can also get a heterogenous dictiona