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

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.

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

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] 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: > >

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,

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

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(, dataBuffer) // Call as if it is a C array It works because of C interoperability compiler magic. As long as the

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

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

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:

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. 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. Here is the

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

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

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 {

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

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

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

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

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

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 =

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 <mark.la...@apple.com> wrote: > > >> On Oct 13, 2016, at 5:37 PM, Hooman Mehr via swift-users >> <swift-users@swift.org <mailto:swift-users@swif

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

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

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

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

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

2016-10-03 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

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

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 <griboz...@gmail.com> wrote: > > On Wed, May 11, 2016 at 1:48 PM, Hooman Mehr via swift-users > <swift-users@swift.org> wrote: >> The reason this is

[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

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