[swift-users] Redeclaration of guard variable is ignored at top-level

2016-06-16 Thread Martin R via swift-users
Hi, I wonder why the Swift compiler does not complain about the redeclaration of `number` after the guard-statement in top-level code: // main.swift import Swift guard let number = Int("1234") else { fatalError() } print(number) // Output: 1234 let number = 5678 print(num

Re: [swift-users] Redeclaration of guard variable is ignored at top-level

2016-06-17 Thread Martin R via swift-users
Filed as https://bugs.swift.org/browse/SR-1804. 2016-06-17 7:17 GMT-07:00 Mark Lacey : > > On Jun 16, 2016, at 10:18 PM, Martin R via swift-users > wrote: > > Hi, > > I wonder why the Swift compiler does not complain about the > redeclaration of `number` after the guar

[swift-users] Permission problem with Swift 3.0 Preview 1 on Ubuntu 14.04

2016-07-05 Thread Martin R via swift-users
I downloaded and installed Swift 3.0 Preview 1 on Ubuntu 14.04, following the instructions in https://swift.org/download/#releases. For some reason, all files in the swift-3.0-PREVIEW-1-ubuntu14.04/usr/lib/swift/CoreFoundation directory of the archive have mode "-rw-r-", i.e. they are not

Re: [swift-users] Cannot invoke 'stride' with an argument list of type '(from: Int, to: Int, by: Int)'

2016-07-05 Thread Martin R via swift-users
It seems that the extension method extension Strideable { public func stride(to end: Self, by stride: Self.Stride) -> StrideTo } from Swift 2.2 is still known to the compiler and only marked as unavailable in Swift 3, as this code example demonstrates: extension Int {

[swift-users] Import C functions as subscript methods

2016-07-11 Thread Martin R via swift-users
SE-0044 ( https://github.com/apple/swift-evolution/blob/master/proposals/0044-import-as-member.md ) describes how to annotate C functions with the swift_name attribute to tell the Swift compiler how an API should be imported. There is an "Amendment: Also allow for importing as subscript." but I

Re: [swift-users] Import C functions as subscript methods

2016-07-11 Thread Martin R via swift-users
Minimal progress: With // Import as subscript float Point3DGetPointAtIndex(Point3D point, int idx) __attribute__((swift_name("getter:Point3D.subscript(self:_:)"))); void Point3DSetPointAtIndex(Point3D point, int idx, float val) __attribute__((swift_name("setter:Point3D.subscrip

Re: [swift-users] Can't initialise using a UTF8 string from within 'withUnsafeBufferPointer' expression

2016-07-18 Thread Martin R via swift-users
This is not an answer to your question, but note that you can pass a Swift String to functions expecting an UnsafePointer (C String) parameter, and the compiler will generate a temporary NUL-terminated UTF-8 representation for you: let io = DispatchIO(type: .stream, path: "/path/to/file", ...

Re: [swift-users] Memory layout of enums

2016-08-01 Thread Martin R via swift-users
I think that is treated in https://github.com/apple/swift/blob/master/docs/ABI.rst, in the sections "Single-Payload Enums" and "Multi-Payload Enums". Martin > On 01 Aug 2016, at 08:36, KS Sreeram via swift-users > wrote: > > Hi > > Is there any place I can read about the memory layout of en

[swift-users] Pointer conversions between different sockaddr types

2016-08-17 Thread Martin R via swift-users
I am trying to figure out how to work correctly with the new UnsafeRawPointer API (from https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md ). Here my concrete example: The connect() system call takes a (const struct sockaddr *address) parameter, but one act

Re: [swift-users] Regression in Xcode8-beta6 Swift?

2016-08-25 Thread Martin R via swift-users
Did that change recently? In Xcode 8 beta 6 let a: AnyObject = UIView() a.hasPrefix("test") still compiles (but of course crashes at runtime). Martin > Am 25.08.2016 um 09:23 schrieb David Hart via swift-users > : > > You can’t call arbitrary functions on AnyObject anymore. Previousl

[swift-users] No hex dump anymore in Data description

2016-08-26 Thread Martin R via swift-users
Is it intentional that the description method of Data returns only the number of bytes, but not a hex dump of the contents (as NSData did)? let data = Data(bytes: [1, 2, 3]) print(data) // 3 bytes print(data.debugDescription) // 3 bytes Of course I can bridge back to NSData:

Re: [swift-users] No hex dump anymore in Data description

2016-08-29 Thread Martin R via swift-users
Done: https://bugs.swift.org/browse/SR-2514 .MartinAm 29.08.2016 um 10:51 schrieb Quinn The Eskimo! via swift-users :On 27 Aug 2016, at 07:45, Martin R via swift-users wrote:Is it intentional that the description method of Data returns only the number of bytes, but not a hex dump of the contents

Re: [swift-users] No hex dump anymore in Data description

2016-08-29 Thread Martin R via swift-users
Done: https://bugs.swift.org/browse/SR-2514 . Martin (Resent as text mail. I apologize for the previous HTML-only mail.) 2016-08-29 10:51 GMT+02:00 Quinn "The Eskimo!" via swift-users : > > On 27 Aug 2016, at 07:45, Martin R via swift-users > wrote: > >> Is it inte

Re: [swift-users] How to create an uninitialized pointer in Swift 3

2016-09-16 Thread Martin R via swift-users
In Swift 3, COpaquePointer was renamed to OpaquePointer, and nullable pointers are represented by optionals: SE-0055 – Make unsafe pointer nullability explicit using Optional https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md So the following sho

Re: [swift-users] NSData vs Data write to string differently?

2016-09-22 Thread Martin R via swift-users
I had filed a related bug https://bugs.swift.org/browse/SR-2514 Add a "hex dump" method to `Data` as a result of the discussion starting at https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160822/003035.html Martin > On 23 Sep 2016, at 03:47, Joe Groff via swift-users > wro

[swift-users] Type inference when assigning the result of reduce to a dictionary

2016-10-04 Thread Martin R via swift-users
I noticed the following when assigning the result of `reduce()` to a dictionary: let array = [1, 2, 3] var dict: [Int: Int] = [:] dict[0] = array.reduce(0, { $0 + $1 }) // (A) // error: binary operator '+' cannot be applied to operands of type 'Int?' and 'Int' // dict[0] = arra

Re: [swift-users] Type inference when assigning the result of reduce to a dictionary

2016-10-04 Thread Martin R via swift-users
> On 4 Oct 2016, at 21:42, Joe Groff wrote: > > >> On Oct 4, 2016, at 5:20 AM, Martin R via swift-users >> wrote: >> >> I noticed the following when assigning the result of `reduce()` to a >> dictionary: >> >> let array = [1

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

2016-11-11 Thread Martin R via swift-users
Nate Cook proposed merging dictionary initializers (with optional conflict resolution) to be added to the standard library: SE-0100 Add sequence-based initializers and merge methods to Dictionary https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-

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

2016-11-23 Thread Martin R via swift-users
I wonder what the best way would be to call a specialized function dependent on the size of `Int`. Let's say that I have two implementations func foo_impl(value: Int32) { /* ... */ } func foo_impl(value: Int64) { /* ... */ } and I want func foo(value: Int) to call the "right one"

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

2016-11-23 Thread Martin R via swift-users
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 >>

[swift-users] Access level of members defined in an extension

2016-12-09 Thread Martin R via swift-users
I have a question about the relationship between the access level of an extension and the access level of members defined in that extension. The "Access Control" chapter in "The Swift Programming Language" states about extensions: Alternatively, you can mark an extension with an explicit ac

[swift-users] Why do collections use the debugDescription on their elements?

2017-01-05 Thread Martin R via swift-users
I wonder why the description method of collections use the debugDescription method to print their elements, and not the description method. Example: let s = "a\"b" print(s) // a"b print(s.debugDescription) // "a\"b" let x = 1.1 print(x) // 1.1 print(x.debugDescription) //

Re: [swift-users] UnsafePointer to UInt64

2017-02-16 Thread Martin R via swift-users
// Int to pointer: let ptr = UnsafePointer(bitPattern: 123)! print(ptr) // 0x007b // Pointer to Int: let int = Int(bitPattern: ptr) print(int) // 123 Int has the same size as a pointer on both 32-bit and 64-bit platforms. Regards, Martin > On 16 Feb 2017, at 14:27, Rien via swift-us

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

2017-02-23 Thread Martin R via swift-users
Is that simplified guaranteed to work? The Swift book says that > As an optimization, when the argument is a value stored at a physical address > in memory, the same memory location is used both inside and outside the > function body. but also > Write your code using the model given by copy-i

[swift-users] UTF-16 string index can be incremented beyond end index

2017-03-16 Thread Martin R via swift-users
The index of the UTF-16 view of a string can be incremented beyond the end index: let s = "foo" let u16 = s.utf16 let i16 = u16.index(u16.endIndex, offsetBy: 99, limitedBy: u16.endIndex) print(i16 as Any) // Optional(Swift.String.UTF16View.Index(_offset: 102)) I wonder if t

Re: [swift-users] String init from Int8 tuple?

2017-04-19 Thread Martin R via swift-users
This reminds me of my previous question https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20170220/004841.html > Is that simplified guaranteed to work? The Swift book says that > > > As an optimization, whe

Re: [swift-users] Guarantees of Tuples as Fixed Sized (stack allocated) Arrays

2017-04-28 Thread Martin R via swift-users
As far as I know, the only guarantee is made for structures imported from C. From https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160516/001968.html : Swift structs have unspecified layout. If you d

Re: [swift-users] Error creating empty array of chained associated type

2017-04-29 Thread Martin R via swift-users
That seems to be a known problem: https://bugs.swift.org/browse/SR-773 > On 29. Apr 2017, at 22:38, Nevin Brackett-Rozinsky via swift-users > wrote: > > I’ve stumbled upon an odd situation where Swift gives a compiler error if I > do things directly, bu

Re: [swift-users] Splitting a string into "natural/visual character" components?

2017-05-12 Thread Martin R via swift-users
The enumerateSubstrings method of (NS)String has a .byComposedCharacterSequences option which causes Emoji sequences like "👨‍👩‍👧‍👦" to be treated as a single unit: func f(_ s: String) -> [String] { var a: [String] = [] s.enumerateSubstrings(in: s.startIndex..https://oleb.net/

[swift-users] Optional binding with non-optional expression

2017-06-02 Thread Martin R via swift-users
This following code fails to compile (which is correct, as far as I can judge that): if let x = 5 { } // error: initializer for conditional binding must have Optional type, not 'Int' But why is does it compile (with a warning) if an explicit type annotation is added? if let y: Int =

Re: [swift-users] Optional binding with non-optional expression

2017-06-04 Thread Martin R via swift-users
print(type(of:y)) // Optional > > You code equals to > > if let y:Int = Int(exactly: 5) { } > > However, I don't know why it did that. Maybe because of type inferring? > > Zhaoxin > > On Fri, Jun 2, 2017 at 8:40 PM, Martin R via swift-users > wrote: >

[swift-users] Passing Data to a f(void *) function

2017-06-30 Thread Martin R via swift-users
I have a C function void myfunc(const void *ptr); which is imported to Swift as func myfunc(_ ptr: UnsafeRawPointer!) This compiles and runs without problems: let data = Data(bytes: [1, 2, 3, 4]) data.withUnsafeBytes { (ptr) in myfunc(ptr) } // (A) and the type of `pt

Re: [swift-users] Passing Data to a f(void *) function

2017-06-30 Thread Martin R via swift-users
> On 30. Jun 2017, at 16:57, Daniel Dunbar wrote: > >> >> On Jun 30, 2017, at 7:40 AM, Martin R via swift-users >> wrote: >> >> I have a C function >> >> void myfunc(const void *ptr); >> >> which is imported to Swift as >>

[swift-users] Exceptional values in the Comparable protocol

2017-07-09 Thread Martin R via swift-users
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 (s

Re: [swift-users] is this a defect in equatable for swift tuples?

2017-07-09 Thread Martin R via swift-users
The == operator is defined for tuples with (up to 6) elements that conform to the Equatable protocol (and < for tuples with Comparable elements): class SomeClass: Equatable { static public func ==(_ lhs:SomeClass, _ rhs:SomeClass) -> Bool { return lhs === rhs }

Re: [swift-users] is this a defect in equatable for swift tuples?

2017-07-09 Thread Martin R via swift-users
> On 9. Jul 2017, at 21:00, Jens Persson via swift-users > wrote: > > Since Array has .elementsEqual, another workaround (until conditional > conformance) is: > > class Tree : Equatable { > let rootData:Int > let children:[(String, Tree)] > > init(rootData: Int, children: [(S

Re: [swift-users] is this a defect in equatable for swift tuples?

2017-07-09 Thread Martin R via swift-users
s:Tree) -> Bool { return lhs.rootData == rhs.rootData && lhs.children.count == rhs.children.count && lhs.children.elementsEqual(rhs.children, by: ==) } } > > But again, that’s a great one to know. > >>

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

2017-07-11 Thread Martin R via swift-users
Thank you for the clarification! > On 10. Jul 2017, at 23:29, Dave Abrahams via swift-users > wrote: > > > 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

[swift-users] Simultaneous access to global tuple members

2017-08-08 Thread Martin R via swift-users
SE-0176 Enforce Exclusive Access to Memory (https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md) states that "Accesses to different stored properties of a struct or different elements of a tuple are allowed to overlap." And indeed this compi

Re: [swift-users] Simultaneous access to global tuple members

2017-08-08 Thread Martin R via swift-users
wrote: > > >> On Aug 8, 2017, at 01:11, Martin R via swift-users >> wrote: >> >> SE-0176 Enforce Exclusive Access to Memory >> (https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md) >> states that "A

Re: [swift-users] Simultaneous access to global tuple members

2017-08-11 Thread Martin R via swift-users
Thank you for the clarification. > On 8. Aug 2017, at 18:39, Guillaume Lessard > wrote: > > >> On Aug 8, 2017, at 09:10, Martin R wrote: >> >> In that example the tuple is a (stored) property of a class, not a global >> variable. And why does it crash for a global variable, but not for a lo

[swift-users] range(of:options:range:locale:) with partial ranges

2017-09-21 Thread Martin R via swift-users
The range(of:options:range:locale:) method of StringProtocol does not accept a partial range as search range. Example (Xcode 9, Swift 4): let str = "foo" let pos = str.startIndex let r = str.range(of: "f", range: pos...) // error: cannot convert value of type 'PartialRangeFrom'

Re: [swift-users] Where to report this bug?

2017-09-22 Thread Martin R via swift-users
But the purpose of func dividedReportingOverflow(by other: Int ) -> (partialValue: Int , overflow: Bool ) is to report an overflow in the return value. And actually this compiles and runs in Xcode 9 if the code is on top-level in main.m: let minusOne = -1 let r1 = Int.min.divide

Re: [swift-users] Where to report this bug?

2017-09-22 Thread Martin R via swift-users
Filed as https://bugs.swift.org/browse/SR-5964 Martin > On 22. Sep 2017, at 12:11, Alex Blewitt wrote: > > Hmm... probably worth filing a bug at https://bugs.swift.org > then with the below test case. > > Alex > >> On 22 Sep

Re: [swift-users] range(of:options:range:locale:) with partial ranges

2017-09-26 Thread Martin R via swift-users
can be used instead. Hoping > for that to be fixed on master soon... Good to know! Regards, Martin > >> On Sep 21, 2017, at 2:44 AM, Martin R via swift-users > <mailto:swift-users@swift.org>> wrote: >> >> The range(of:options:range:locale:) method of StringProt

Re: [swift-users] How do you declare a custom NSAttributedStringKey in Objective-C for Swift use?

2017-09-27 Thread Martin R via swift-users
Could it be that this is (just) a problem of the generated interface _view_ ? With extern NSAttributedStringKey ODRolloverTokenAttributeName; in an Objective-C header the "Generated Interface" is displayed as public static let ODRolloverTokenAttributeName: NSAttributedStringKey as you n

[swift-users] Why does the withUnsafeMutableBufferPointer closure take an inout parameter?

2017-10-08 Thread Martin R via swift-users
I wonder why the closure in the Array method mutating func withUnsafeMutableBufferPointer(_ body: (inout UnsafeMutableBufferPointer) throws -> R) rethrows -> R takes an _inout_ parameter. The closure is called with a pointer to the contiguous array storage, and I fail to imagine an example

Re: [swift-users] Why does the withUnsafeMutableBufferPointer closure take an inout parameter?

2017-10-10 Thread Martin R via swift-users
let` not a `var` and hence you can't > call mutating methods on it. There is no 'invar' in Swift, best you can do is > `inout`. > > -- Howard. > > On 9 October 2017 at 06:14, Martin R via swift-users > wrote: > I wonder why the closure in th

Re: [swift-users] Why does the withUnsafeMutableBufferPointer closure take an inout parameter?

2017-10-11 Thread Martin R via swift-users
> On 11. Oct 2017, at 22:11, Guillaume Lessard > wrote: > > >> On Oct 10, 2017, at 01:50, Martin R via swift-users >> wrote: >> >> Thank you Howard for you response! However, I have a follow-up question: >> >> Why are UnsafeMutableB

Re: [swift-users] Why does the withUnsafeMutableBufferPointer closure take an inout parameter?

2017-10-14 Thread Martin R via swift-users
> On 14. Oct 2017, at 13:15, Geordie Jay via swift-users > wrote: > > Chris Lattner schrieb am Sa. 14. Okt. 2017 um 05:18: > >> On Oct 13, 2017, at 7:40 PM, Andrew Trick via swift-users >> wrote: >> >> >> >>> On Oct 12, 2017, at 3:52 AM, Geordie Jay via swift-users >>> wrote: >>> >>>

Re: [swift-users] Swift 4 Strings and flatMap

2017-10-21 Thread Martin R via swift-users
Compare https://bugs.swift.org/browse/SR-1570 : Swift can infer the return type from single-statement closures, but not from multi-statement closures. Therefore in let result = strArr.flatMap { x in return x } the closure type is inferred as (String)-> String, and therefore th

Re: [swift-users] Was the String contains(_:) method changed in Swift 4?

2017-11-08 Thread Martin R via swift-users
String has also a func contains(_ other: T) -> Bool where T : StringProtocol method, but apparently only if Foundation is imported. > Am 08.11.2017 um 04:03 schrieb David Keck via swift-users > : > > I am looking at the latest version of the “App Development with Swift” iBook > on page 12

Re: [swift-users] Was the String contains(_:) method changed in Swift 4?

2017-11-08 Thread Martin R via swift-users
Unicode correct string comparison _is_ tricky, but `s.contains(s)` will always be true :) And `s.contains(ss)` with your example strings return false. Regards, Martin > Am 08.11.2017 um 09:56 schrieb Quinn The Eskimo! via swift-users > : > > > On 8 Nov 2017, at 08:47, Mar

Re: [swift-users] Modulo operation in Swift?

2017-11-09 Thread Martin R via swift-users
There was a discussion in swift-evolution, starting at https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160516/018521.html about adding a "true modulo" operator to the standard library. There were different opinions whether - such a thing needs to be in the standard library at

Re: [swift-users] Cast CFString to String

2017-12-05 Thread Martin R via swift-users
Another workaround would be public init(uti: String) { switch uti.lowercased() { case String(kUTTypeText): self = .text default: self = .unknown } } because there is a `String(_ cocoaString: NSString)` initializer in Foundation. Reg

[swift-users] Question about "lazy initialization" in CommandLine.arguments documentation

2018-01-11 Thread Martin R via swift-users
The `CommandLine` documentation (https://developer.apple.com/documentation/swift/commandline) states about the `arguments` property: ... also use lazy initialization of static properties to safely initialize the swift arguments. Apparently this originates from the markup comments in https