[swift-corelibs-dev] Scanner with Swift 3
Oliver Drobnik
oliver at cocoanetics.com
Mon Dec 5 12:55:34 CST 2016
Hello,
This is a repeat of an earlier post where I did not get a single response... might have fallen through the cracks...
Earlier I posted this proposal to the Swift Evolution Mailing list. Then I looked at the NSScanner implementation in core libs and found experimental API using the return value to for returning the scanned results. See my comments on that below:
—
Working on a function for Foundation’s Scanner I stumbled on this LLVM crash: https://bugs.swift.org/browse/SR-3295
This got me thinking about a workaround and I would like to prose this:
When importing Foundation into Swift 3, all
AutoreleasingUnsafeMutablePointer<T?>?
should instead be exposed as simple:
inout T?
e.g.
open func scanString(_ string: String, into result: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool
would become
open func scanString(_ string: String, into result: inout String?) -> Bool
The call would stay exactly the same for normal use cases where you specify a receiving variable:
var string: String?
scanString("=", into: &string)
because inout parameters require a &
for the use case where you don’t require a receiving parameter, a second method without result parameter would be generated:
open func scanString(_ string: String) -> Bool
This is necessary because you cannot specify nil or an immutable value for an inout parameter.
A fixit/migration would change calls to
scanString(“foo", into result: nil)
into
scanString(“foo")
The normal call with receiving variable would stay the same. But the case without return would become more concise.
What do you think?
kind regards
Oliver Drobnik
—
The new experimental API does not consider the situation that one might just want to scan a token in a long string and since I know what I am scanning I don’t care about the result. For example in my scanning I would like to processed if I hit a “foo“ …
The experimental API would force me to do this:
if let _ = scanString(“foo“) { }
My proposal is more elegant:
if scanString(“foo”) {}
And if you do lots and lots of these scans then performance would benefit from the compiler not having to pack the return for an autoreleasing value which would have to be released right away due to assignment to _.
IMHO my proposal is more in line with the spirit of the original API: 1) return Bool if the scan was successful, 2) optionally return the scanned value.
kind regards
Oliver Drobnik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20161205/00fa8210/attachment.html>
More information about the swift-corelibs-dev
mailing list