Re: [swift-users] Recommendation for thread-safe dictionary

2015-12-10 Thread Howard Lovatt via swift-users
I would do something like: import Foundation class Task { // Must be a class to prevent Swift copy semantics eliminating the result private static let queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0) private let group = dispatch_group_create()

Re: [swift-users] Object size on heap

2016-03-23 Thread Howard Lovatt via swift-users
nquiries: > > let required_size = malloc_size(unsafeAddressOf(*object_reference*)) > let actual_size = malloc_good_size(required_size) > > > Kate Stone k8st...@apple.com >  Xcode Low Level Tools > > On Mar 23, 2016, at 3:59 PM, Howard Lovatt via swift-users < > swift-users@swift

Re: [swift-users] Object size on heap

2016-03-23 Thread Howard Lovatt via swift-users
board. For >> fine-grained inquiries about memory use you can also rely on malloc family >> functions to make inquiries: >> >> let required_size = malloc_size(unsafeAddressOf(*object_reference*)) >> let actual_size = malloc_good_size(required_size) >> >> &

[swift-users] Strange type error

2016-03-20 Thread Howard Lovatt via swift-users
HI, Does anyone know what is happening here: protocol Subscriptable { associatedtype Element associatedtype Index var firstIndex: Index { get } var lastIndex: Index { get } subscript(index: Index) -> Element { get set } } struct AllSubscriptable: Subscriptable {

Re: [swift-users] Static type when expecting dynamic type

2017-02-02 Thread Howard Lovatt via swift-users
t. > > I’ll have a fix shortly. > > Slava > > On Feb 2, 2017, at 9:04 AM, Jordan Rose via swift-users < > swift-users@swift.org> wrote: > > Seems like a bug, and moreover it seems like a regression from Swift 3.0. > Mind filing a report at bugs.swift.org? >

[swift-users] Static type when expecting dynamic type

2017-02-01 Thread Howard Lovatt via swift-users
Hi All, Anyone know what is going on here: //: Closure picks up static type not dynamic class MutableReference { init() { guard type(of: self) != MutableReference.self else { fatalError("MutableReference is an abstract class; create a derrivative of

Re: [swift-users] [swift-evolution] for-else syntax

2017-02-03 Thread Howard Lovatt via swift-users
Why not add to the library: extension Sequence { func forEach(_ eacher: (Iterator.Element) throws -> Void, elser: () throws -> Void) rethrows { var hasIterated = false for element in self { hasIterated = true try eacher(element) } guard

Re: [swift-users] [swift-evolution] for-else syntax

2017-02-05 Thread Howard Lovatt via swift-users
LoopControls.returnValue(42.0) } // for _ in a { return 42.0 } } catch LoopControls.returnValue(let value) { return value } On Sun, 5 Feb 2017 at 2:32 pm, Rob Mayoff via swift-users < swift-users@swift.org> wrote: > On Fri, Feb 3, 2017 at 7:15 PM, Howard Lovatt via swift-users < &

Re: [swift-users] Associativity of && and || operator

2017-02-20 Thread Howard Lovatt via swift-users
To me it would be surprising if && grouped differently than * or &; since it is closely associated with boolean-and, which in turn is the equivalent operation to multiply in Boolean logic. On Fri, 17 Feb 2017 at 7:56 pm, rintaro ishizaki via swift-users < swift-users@swift.org> wrote: > Hello

Re: [swift-users] The case of broken polymorphism or "Cannot convert value of type to expected argument type"?

2017-02-20 Thread Howard Lovatt via swift-users
It is confusing in Swift what can be covariant and what is invariant, consider: // Covarant arrays work class A {} class B: A {} let a = A() // A let b = B() // B var arrayA = [a] // Array arrayA[0] = b // OK // And arrays of arrays var arrayArrayA = [arrayA]

Re: [swift-users] Weak references in generic types

2016-08-31 Thread Howard Lovatt via swift-users
Playing around I found that if you make the protocol @objc instead of AnyObject then it works :). EG: struct WeakReference { weak var value: T? } @objc protocol P { // Note @objc, class or AnyObject does not work var i: Int { get } } class CP: P { var i: Int = 0 } let weakPs:

Re: [swift-users] Weak references in generic types

2016-09-01 Thread Howard Lovatt via swift-users
n 1 Sep 2016, at 03:23, Howard Lovatt via swift-users < > swift-users@swift.org > <javascript:_e(%7B%7D,'cvml','swift-users@swift.org');>> wrote: > > Playing around I found that if you make the protocol @objc instead of > AnyObject then it works :). EG: > > stru

[swift-users] Weak references in generic types

2016-08-29 Thread Howard Lovatt via swift-users
Hi, I am wanting to use weak references in generic data structures; in the example below Array, but in general any generic type. I can almost get it to work :( My experiments started off well; the following works: // Array of weak references OK struct WeakReference { weak var value: T? }

Re: [swift-users] hello, first time poster and accelerate question

2016-11-21 Thread Howard Lovatt via swift-users
Take a look at the Surge library. On Tue., 22 Nov. 2016 at 4:24 am, Kenny Leung via swift-users < swift-users@swift.org> wrote: > If only you could write this directly in Swift and have it use accelerate… > > -Kenny > > > > On Nov 18, 2016, at 8:07 PM, Jacob Bandes-Storch via swift-users < >

Re: [swift-users] Workaround for generics not currently supporting conditional conformance to a protocol

2016-11-16 Thread Howard Lovatt via swift-users
rd. > > -- Howard. > > On 16 November 2016 at 16:35, David Sweeris <daveswee...@mac.com> wrote: > >> >> > On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users < >> swift-users@swift.org> wrote: >> > >> > Hi All

[swift-users] Fwd: Workaround for generics not currently supporting conditional conformance to a protocol

2016-11-15 Thread Howard Lovatt via swift-users
- Howard. On 16 November 2016 at 16:35, David Sweeris <daveswee...@mac.com> wrote: > > > On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users < > swift-users@swift.org> wrote: > > > > Hi All, > > > > Does anyone have a good workaround for generics

[swift-users] Workaround for generics not currently supporting conditional conformance to a protocol

2016-11-15 Thread Howard Lovatt via swift-users
Hi All, Does anyone have a good workaround for generics not currently supporting conditional conformance to a protocol. As stated in the Generics Manifesto something like this would be nice: extension Array: Equatable where Element: Equatable { static func ==(lhs: Array, rhs: Array) ->

Re: [swift-users] Changing precedence of / operator for my protocol?

2016-11-29 Thread Howard Lovatt via swift-users
Why not define some other symbol so that you can get the precedence you want? -- Howard. On 30 November 2016 at 09:28, Greg Parker via swift-users < swift-users@swift.org> wrote: > > > On Nov 29, 2016, at 2:55 AM, Rick Mann via swift-users < > swift-users@swift.org> wrote: > > > > Working on

Re: [swift-users] [swift-evolution] Best way to handle escaping function that might throw

2017-01-11 Thread Howard Lovatt via swift-users
Another possibility, other than generics, would be to drop rethrows all together and have the compiler infer if a throw is possible or not, including: struct FStore { let f: () throws -> Void func call() throws { try f() } } The compiler can make two versions, one if f

Re: [swift-users] [swift-evolution] Best way to handle escaping function that might throw

2017-01-11 Thread Howard Lovatt via swift-users
Yes, I have used Result in the past. In this case I wanted to use throws because that is what functions like map do. Looks like I will have to stick with throwing, not too bad since the overhead is reasonably low. On Wed, 11 Jan 2017 at 8:03 am, T.J. Usiyan wrote: > I

Re: [swift-users] [swift-evolution] Best way to handle escaping function that might throw

2017-01-12 Thread Howard Lovatt via swift-users
@Slava, I'm imagining that the compiler does much the same as Anton suggested, e.g. Anton's: struct FStore { let f: () throws -> Void init(_ f: @escaping () throws -> Void) { self.f = f } func call() throws { try f() } } Is much the same as my: struct FStore

Re: [swift-users] [swift-evolution] Best way to handle escaping function that might throw

2017-01-12 Thread Howard Lovatt via swift-users
@Anton, Yes that would work and we would get typed throws, which I like. As an aside, I think E would have to be constrained to be an Error, ``, and maybe `throws` reads better because you are used to types in angle brackets. -- Howard. On 13 January 2017 at 08:32, Anton Zhilin

Re: [swift-users] Dimensional analysis

2016-11-30 Thread Howard Lovatt via swift-users
Another feature I would like would be a dimensions wrapper for an array. So that I can mark all values in the array as having the same units, rather than mark each individual element as having a particular unit. It is conceivable that wrapping each element, both performance and memory wise, is OK

Re: [swift-users] Weird protocol behaviour.

2016-12-22 Thread Howard Lovatt via swift-users
, at 2:17 PM, Howard Lovatt via swift-users < > swift-users@swift.org> wrote: > > I suspect a compiler bug since A is a P. The equivalent in Java works: > > interface P {} > class X implements P {} > > > void foo(A x) {} > > > void bar() { > final P x

Re: [swift-users] [swift-evolution] Best way to handle escaping function that might throw

2017-01-14 Thread Howard Lovatt via swift-users
nt. > > Does that mean that Never should be a subtype of Error (maybe it is > already) ? Else you couldn't say throws(Never) or FSTore > > @Howard > I'm not sure about <>, it should stay reserved for generics, IMHO. > > Pierre > > Le 13 janv. 2017 à 01:11, Howard

[swift-users] Are all extensions from all modules exported on mass?

2017-04-19 Thread Howard Lovatt via swift-users
Hi, If I have the *same* extension in two *different* modules, ModuleA and ModuleB, and ModuleC uses both modules, but in *seperate* files, then there is still ambiguity about which extension to call: ModuleA A.swift public protocol P { func m() -> String } extension Int: P { public func

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-19 Thread Howard Lovatt via swift-users
You are hardly alone struggling with this, it seems to come up every other week! You can write your own custom AnyProtocol type that includes Self, a pain but doable, e.g.: protocol A { func a() -> String } protocol B { func b() -> String } struct AB1: A, B, Hashable { func a() ->

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Howard Lovatt via swift-users
I would be tempted to use classes for this if you can use single inheritance. If you need multiple inheritance then use an enum and hand code the dispatch, a lot more work :(. E.G.: protocol A { func a() -> String } protocol B { func b() -> String } struct AB1: A, B, Hashable { func

Re: [swift-users] Is it possible to specify error type thrown in a protocol method

2017-07-06 Thread Howard Lovatt via swift-users
On balance I think I prefer result types. Java's fully typed throws and Swift's semi-typed throws are fine though. I can't see Swift supporting result types because they already have throws. -- Howard. > On 6 Jul 2017, at 4:27 pm, Tim Wang wrote: > > Thanks

Re: [swift-users] Is it possible to specify error type thrown in a protocol method

2017-07-05 Thread Howard Lovatt via swift-users
You could use a result type, e.g.: https://github.com/antitypical/Result Or roll your own result type. I think the reason that Swift doesn't support what you want is because of rethrows. When you declare a method as rethrows it can throw anything because the closure it is re-throwing can throw

Re: [swift-users] Help! Slicing an array is very expensive

2017-05-09 Thread Howard Lovatt via swift-users
I find trimming relative to appending OK on my 6 year old MacBook Pro. EG: import Foundation func elapsed(s: DispatchTime, e: DispatchTime) -> Double { return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000 } let s = 250_000_000 var a = [UInt8]() a.reserveCapacity(s) let

Re: [swift-users] Help! Slicing an array is very expensive

2017-05-09 Thread Howard Lovatt via swift-users
My mistake. If I create a new array I get the problem. EG: import Foundation func elapsed(s: DispatchTime, e: DispatchTime) -> Double { return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000 } let s = 250_000_000 var a = [UInt8]() a.reserveCapacity(s) let sa =

Re: [swift-users] override-like keyword for default implementations

2017-05-16 Thread Howard Lovatt via swift-users
In the proposal I just posted in reply to Jordan Rose all these examples would work as expected, though the syntax and/or semantics would be slightly different: Example 1 In R.swift protocol R { func f() } // f is abstract In P.swift protocol P {} // f cannot be in protocol and extension

Re: [swift-users] override-like keyword for default implementations

2017-05-16 Thread Howard Lovatt via swift-users
Here is a proposal I put forwards last month that addresses the issue (and others), the original versions did not allow retroactive modeling but following feedback it was incorporated. PS Bitten by this problem two days ago, copied some example Swift 2 code into a Swift 3 project and an obj-c

Re: [swift-users] Help! Slicing an array is very expensive

2017-05-09 Thread Howard Lovatt via swift-users
and hence doesn't require wrapping. The append method is a little slower though. -- Howard. On 10 May 2017 at 04:32, Michael Gottesman <mgottes...@apple.com> wrote: > Could you file a bug report? bugs.swift.org? > > Michael > > On May 9, 2017, at 12:59 AM, Howard Lovatt vi

Re: [swift-users] any wisdom about sharing "common" overloads/extensions in base libraries?

2017-06-20 Thread Howard Lovatt via swift-users
There is a *bug* that the Swift people know about, but you are **meant** to be able to do this: ModuleA A.swift public protocol P { func m() -> String } extension Int: P { public func m() -> String { return "AP.m" } } ModuleB B.swift public protocol P { func m() -> String } extension

Re: [swift-users] any wisdom about sharing "common" overloads/extensions in base libraries?

2017-06-21 Thread Howard Lovatt via swift-users
Jordon Rose from Apple said: "It's tracked by SR-3908 . (The description's a little different but it's the same underlying issue.)" -- Howard. On 21 June 2017 at 12:07, David Baraff wrote: > > On Jun 20, 2017, at 6:59 PM,

[swift-users] Robust Codable coding

2017-06-23 Thread Howard Lovatt via swift-users
Hi All, I have started to use Codable and was looking for some advice. I want to make the persisted data structure robust so that if I change the properties as the code develops the new code can deserialise an old saved file. This is what I am currently doing: struct Project: Codable {

Re: [swift-users] Restricting associated values

2017-06-18 Thread Howard Lovatt via swift-users
To me Angle is a unit with two common representations: radians and degrees. It's not an enum because it doesn't have two values, it has one value that you can view in two ways. Therefore I would make an Angle struct, something like: //: Angle struct instead of angle enum import Foundation

Re: [swift-users] How to use a protocol to infer a generic type with constraints

2017-05-02 Thread Howard Lovatt via swift-users
Another +1 from me, a real pain -- Howard. > On 3 May 2017, at 4:05 am, David Sweeris via swift-users > wrote: > > >> On May 2, 2017, at 10:16 AM, Edward Connell via swift-users >> wrote: >> >> Does anyone have an idea when this is going to be

Re: [swift-users] overloading methods where only difference is optional vs. non-optional type

2017-10-10 Thread Howard Lovatt via swift-users
func funny() -> Int { return 1 } func funny() -> String { return "2" } let i: Int = funny() let s: String = funny() Is fine. Since Swift knows the return type required. However, it can’t infer the type of: let q = funny() -- Howard. > On 11 Oct 2017, at 4:36 am, C. Keith Ray via

[swift-users] Is this a compiler bug?

2017-09-04 Thread Howard Lovatt via swift-users
Hi All, Is this a compiler bug? struct Box { var value: T init(_ value: T) { self.value = value } /// Unboxing operator. static func >> (left: Box, right: inout T?) { right = left.value } } var u: String? let b = Box("Test") b >> // ERROR: Cannot convert value of

Re: [swift-users] Is this a compiler bug?

2017-09-04 Thread Howard Lovatt via swift-users
Thanks for prompt reply - link is: https://bugs.swift.org/browse/SR-5838 -- Howard. On 5 September 2017 at 11:58, Slava Pestov <spes...@apple.com> wrote: > Yeah, looks like a bug. Do you mind filing a JIRA? > > Slava > > On Sep 4, 2017, at 8:55 PM, Howard Lovatt via

[swift-users] Custom operators from a module

2017-09-06 Thread Howard Lovatt via swift-users
Hi All, I am trying to use a custom operator imported from a module, unfortunately you can't add public to the operators definition and therefore it isn't exported from the module. I can manually redefine the operator in the module where the operator is used, but that isn't very satisfactory -

Re: [swift-users] Custom operators from a module

2017-09-06 Thread Howard Lovatt via swift-users
ion of logging a bug, reducing the problem to bug form really helped. -- Howard. On 7 September 2017 at 07:36, Howard Lovatt via swift-users < swift-users@swift.org> wrote: > I have a custom operator defined in a module and in my XCTest I import > that module. In the tests th

Re: [swift-users] Custom operators from a module

2017-09-06 Thread Howard Lovatt via swift-users
; > Jordan > > >> On Sep 6, 2017, at 02:08, Howard Lovatt via swift-users >> <swift-users@swift.org> wrote: >> >> Hi All, >> >> I am trying to use a custom operator imported from a module, unfortunately >> you can't add public to the op

Re: [swift-users] Initialization Catch-22?

2017-09-26 Thread Howard Lovatt via swift-users
I know were you are coming from, but: 1. In other languages it would be the equivalent of an force unwrapped optional and you could get a NullPointerException (or worse - core dump). Using a force unwrapped optional just levels the playing field! 2. You could make it private(set) which would

Re: [swift-users] Comprehensive documentation for Collection?

2017-12-15 Thread Howard Lovatt via swift-users
It’s a problem with Apple documentation. swiftdoc.org does list the associated types, but is no longer maintained :(. -- Howard. > On 16 Dec 2017, at 1:17 pm, Nevin Brackett-Rozinsky via swift-users > wrote: > > In Xcode if I write, > > extension Collection { >

Re: [swift-users] FileManager.copyItem not throwing?

2017-11-11 Thread Howard Lovatt via swift-users
I don’t have any inside knowledge on this, i.e. I am not certain I am correct, but my understanding was that you were meant to let it throw and catch the error rather than test before hand. -- Howard. > On 11 Nov 2017, at 12:51 pm, Marco S Hyman via swift-users >

Re: [swift-users] A confusing protocol extension behaviour/bug

2018-01-01 Thread Howard Lovatt via swift-users
ard Lovatt via swift-users > <swift-users@swift.org> wrote: > > Unfortunately the error messages you get with protocols are limited :). The > compiler is doing the right thing, no bug. The problem is that it is really > easy to mak > > -- Howard. > >> On 1 Jan

Re: [swift-users] A confusing protocol extension behaviour/bug

2018-01-01 Thread Howard Lovatt via swift-users
Unfortunately the error messages you get with protocols are limited :). The compiler is doing the right thing, no bug. The problem is that it is really easy to mak -- Howard. > On 1 Jan 2018, at 3:40 pm, Toni Suter via swift-users > wrote: > > Hi Marc, > > There are

Re: [swift-users] Rethrows issue

2018-01-01 Thread Howard Lovatt via swift-users
I don’t get why that type checks: 1. rescue always throws inside sync, therefore 2. _syncHelper inside sync always throws, therefore 3. sync always throws, therefore 4. Shouldn’t sync be declared throws not rethrows? -- Howard. On 1 Jan 2018, at 1:10 pm, Charles Srstka via swift-users