In Swift2.2, I have an extension to Optional that looks like:
extension Optional {
func ifNotNil<T>(_ closure:(Wrapped) -> T) -> T? {
switch self {
case .some (let wrapped):
return closure(wrapped)
case .none:
return nil
}
}
}
It allows for code like
anImageView.image = self.something.ifNotNil { self.getImageFor($0) }
But sometimes, I don't care about the result:
myBSON["key"].string.ifNotNil {
print($0}
}
In Swift2.2, it worked like a charm. But firing up the new XCode8 Beta and converting to Swift3, I'm getting warnings anywhere that I do the second type. It's almost as if there's an implicit #warn_unused_result. Is this just an early beta bug? Or something I can no longer do in Swift3? Or something I need to newly fix in Swift3?
You can discard the result using:
_ = myBSON["key"].string.ifNotNil {
print($0}
}
Or mark your method to not warn for unused results:
extension Optional {
#discardableResult func ifNotNil<T>(_ closure:(Wrapped) -> T) -> T? {
switch self {
case .some (let wrapped):
return closure(wrapped)
case .none:
return nil
}
}
}
Reference : SE-0047
Related
I want to implement the following:
throwingFunction()??.doStuff()
/* if throwingFunction throws an error:
print the error
else
returns an object with the doStuff() Method
*/
throwingFunction()??
/*
if an error is thrown,
prints the error.
else
execute the function without errors.
*/
I'm not sure where to look in the source code for examples on how do, try, catch were implemented. The Swift error docs explain how to use error handle methods that are already implemented. To be clear, I want to implement custom error handling with the above syntax.
Something like:
precedencegroup Chaining {
associativity: left
}
infix operator ?? : Chaining
extension Result {
// ERROR: Unary operator implementation must have a 'prefix' or 'postfix' modifier
static func ??(value: Result<Success, Failure>) -> Success? {
switch value {
case .success(let win):
return win
case .failure(let fail):
print(fail.localizedDescription)
return nil
}
}
}
You can define a postfix operator which takes a throwing closure as (left) operand. ?? is already defined as an infix operator, therefore you have to choose a different name:
postfix operator <?>
postfix func <?><T>(expression: () throws -> T) -> T? {
do {
return try expression()
} catch {
print(error)
return nil
}
}
Now you can call
let result = throwingFunc<?>
or chain it with
let result = (throwingFunc<?>)?.doStuff()
Previous answer:
?? is already defined as an infix operator. For a postfix operator you have to choose a different name, for example:
postfix operator <?>
extension Result {
static postfix func <?>(value: Result) -> Success? {
switch value {
case .success(let win):
return win
case .failure(let fail):
print(fail.localizedDescription)
return nil
}
}
}
Now you can call
let res = Result(catching: throwingFunc)<?>
or chain it with
let res = (Result(catching: throwingFunc)<?>)?.doStuff()
There is little chance that you can do this, without actually forking apple/swift and creating your own version of the compiler... Here are my attempts:
First, I noticed that the second part of the desired result, ?.doStuff() looks exactly like a optional chaining expression. I thought I could make a postfix ? operator that returned an optional. But it turns out, I can't declare an ? operator at all:
postfix operator ? // error
So I used a visually similar character - ‽ - instead. The type of a throwing function is () throws -> Void and I used #autoclosure so that the {} can be omitted:
typealias ThrowingFunction<T> = () throws -> T
postfix operator ‽
postfix func ‽<T>(lhs: #autoclosure ThrowingFunction<T>) -> T? {
switch Result(catching: lhs) {
case .failure(let error):
print(error.localizedDescription)
return nil
case .success(let t):
return t
}
}
// Usage:
func f() throws -> Int {
throw URLError(URLError.badURL)
}
// You have to use it like this :(
(try f()‽)
(try f()‽)?.description
The try could be omitted if the function you are calling takes no arguments:
f‽
(f‽)?.description
To make functions of other arity work without try, you need to create an implementation of ‽ for each arity, which sucks.
But the brackets must be there because of how Swift parses operators :(
Then I tried to make the approach you attempted, with key paths:
func ??<T, U>(lhs: #autoclosure ThrowingFunction<T>, rhs: KeyPath<T, U>) -> U? {
switch Result(catching: lhs) {
case .failure(let error):
print(error.localizedDescription)
return nil
case .success(let t):
return t[keyPath: rhs]
}
}
func f() throws -> Int {
throw URLError(URLError.badServerResponse)
}
This seems to be even worse, because you gotta use it like this:
try f() ?? \.description
You can't omit the try,
f ?? \.description // type inferencer freaks out, thinks T is ThrowingFunction<Int>
nor reduce the spaces on either side of ?? (See here for why):
try f()??\.description
Plus there is this backlash that is an integral part of the keypath syntax, and you can only use it for key paths, not methods. :(
Summary
You can't do this because:
You can't overload ?
You can't put a ? right after anything because it will be parsed as optional chaining
You must write try, unless you cater for every arity.
postfix operator *
#discardableResult
postfix func *<Preferred>(expression: ErrorAlt<Preferred>) -> Preferred? {
switch expression {
case .preferred(let pref):
return pref
case .error(let err):
print(err.localizedDescription)
return nil
case .initializersWereNil:
print("initializersWereNil")
return nil
}
}
Here is an example usage.
enum TestMeError: Error {
case first
}
extension Int {
func printWin() {
print("we did it!")
}
}
func testMe() -> ErrorAlt<Int> {
if true {
return .error(TestMeError.first)
} else {
return .preferred(40)
}
}
// USAGE
testMe()*
I'm trying to use Swift's new Result type in such a way that the type of the .success associated value is a generic. The rather contrived sample code below works, but is there a way to simplify the type casting so that the compiler can infer the correct type for T?
enum FetchError : Error {
case unknownKey
}
enum FetchKey {
case getWidth
case getName
}
func fetchValue<T>(_ key:FetchKey) -> Result<T, FetchError> {
switch key {
case .getName:
// Ideally I would like to just use: return .success("Johnny Appleseed")
return Result<String, FetchError>.success("Johnny Appleseed") as! Result<T, FetchError>
case .getWidth:
return Result<Double, FetchError>.success(25.0) as! Result<T, FetchError>
#unknown default:
return .failure(.unknownKey)
}
}
// This explicit type declaration is also required.
let r:Result<String, FetchError> = fetchValue(.getName)
print(r)
To your question about the type-casting, you can definitely simplify it:
case .getName:
return .success("Johnny Appleseed" as! T)
This is ok if asking for the wrong type should be considered a programming error (and so should crash), and the results will never come from external sources. If the data could ever come from an external source, then you should never crash in response to it being wrong.
In that case, we should model that kind of error:
enum FetchError : Error {
case unknownKey
case invalidType
}
Then you can have a syntax very close to what you like by adding a function to do the (possibly failing) type conversion:
func fetchValue<Value>(_ key:FetchKey) -> Result<Value, FetchError> {
func checkType(_ value: Any) -> Result<Value, FetchError> {
guard let value = value as? Value else { return .failure(.invalidType) }
return .success(value)
}
switch key {
case .getName: return checkType("Johnny Appleseed")
case .getWidth: return checkType(25.0)
#unknown default: return .failure(.unknownKey)
}
}
That said, I'd do it this way to avoid the ugliness of required type annotations:
func fetch<Value>(_: Value.Type, forKey key: FetchKey) -> Result<Value, FetchError> { ... }
let r = fetch(String.self, forKey: .getName)
This follows the pattern of Codable.
Here's the whole solution together in one place in a few different ways:
Returning Result
enum FetchError : Error {
case unknownKey
case invalidType
}
enum FetchKey {
case width
case name
}
func fetch<Value>(_: Value.Type, forKey key: FetchKey) -> Result<Value, FetchError> {
func checkType(_ value: Any) -> Result<Value, FetchError> {
guard let value = value as? Value else { return .failure(.invalidType) }
return .success(value)
}
switch key {
case .name: return checkType("Johnny Appleseed")
case .width: return checkType(25.0)
#unknown default: return .failure(.unknownKey)
}
}
With throws
I think this gets a little nicer if you throw rather than wrapping things into Result. It means you can more easily lift checkType into one place and gets very close to the syntax you said you wanted.
func fetch<Value>(_: Value.Type, forKey key: FetchKey) throws -> Value {
func checkType(value: () throws -> Any) throws -> Value {
guard let value = try value() as? Value else { throw FetchError.invalidType }
return value
}
return try checkType {
switch key {
case .name: return "Johnny Appleseed"
case .width: return 25.0
#unknown default: throw FetchError.unknownKey
}
}
}
With Optionals
This gets a little simpler with Optional if you don't really care about the errors.
func fetch<Value>(_: Value.Type, forKey key: FetchKey) -> Value? {
func _fetch() -> Any? {
switch key {
case .name: return "Johnny Appleseed"
case .width: return 25.0
#unknown default: return nil
}
}
return _fetch() as? Value
}
The function you are trying to create has a dynamically typed generic. This isn't possible as the Swift compiler needs to know the types for each method/variable at compile time.
Suppose you have
func whatToFetch() -> FetchKey {
// Randomly returns one of the FetchKey cases
}
let r = fetchValue(whatToFetch())
There is no way for the compiler to know the type of r before the call is made. Your example code works because your force-casts are perfectly lined up. But in a correct implementation you should not have to specify what type T is inside your generic function
In your case, it seems generics are not really needed and you could do away with protocols:
enum FetchKey {
case someStringValueKey
case someDoubleValueKey
case someModelValueKey
}
protocol FetchResult {}
extension String: FetchResult {}
extension Double: FetchResult {}
extension SomeModel: FetchResult {}
func fetch(_ key: FetchKey) -> Result<FetchResult,Error> {
switch key {
case .someStringValueKey:
return .success("")
case .someDoubleValueKey:
return .success(1.0)
case .someModelValueKey:
return .success(SomeModel())
}
}
let wtf = whatToFetch()
let r = fetch(wtf)
switch r {
case .success(let value):
switch wtf {
case .someStringValueKey:
guard let stringValue = value as? String else { return }
case .someDoubleValueKey:
guard let doubleValue = value as? Double else { return }
case .someModelValueKey:
guard let someModelValue = value as? SomeModel else { return }
}
case .failure(let error):
print("Better do more than just print on production code")
}
Trying to use an unqualified generic fetchValue<T> to do what you want probably isn’t going to work. The reason is that in a generic function, the T is specified by the caller, not the function. You’re essentially saying, “Ask fetchValue for any T you want, and it will give you a result.”
There’s missing link in the way you’ve set up your types. The red flag that’s your primary clue to this is the use of as!. That’s a sign that you are making assumptions about type relationships that you’re not telling the compiler about.
What does that mean? Well, note that with your code, let r: Result<URLConnection, FetchError> = fetchValue(.getName) also compiles — but then crashes at runtime!
One solution is to have an umbrella type that gathers all the possible result types, as Emil’s solution does. In this approach, you erase the type of the result, and ask callers to dynamically extract it. Callers have to deal with the possibility that the result might be of any type, not necessarily the one they expected. This is a common pattern when dealing with dynamically structured data such as JSON documents.
A second approach is to do what Rob suggests: let the caller specify the type, but translate an incorrect type into an error.
A third approach to solving this is to find a way to associate keys with result types in the type system, i.e. to tell the compiler getName → String, getWidth → Double. Unfortunately, AFAIK, there’s no way to do that with individual enum cases, so you’ll need to encode the keys as something other than an enum.
Here’s one way to do it:
enum FetchError : Error {
case unknownKey
}
protocol FetchKey {
associatedtype ValueType
func get() -> ValueType
}
struct GetNameKey: FetchKey {
func get() -> String { // This line establishes the getName → String mapping
return "Johnny Appleseed"
}
}
struct GetWidthKey: FetchKey {
func get() -> Double { // This line establishes the getWidth → Double mapping
return 25.0
}
}
// This function signature means: “If you give fetchValue a key, it
// will give you a result _with the appropriate type_ for that key”
//
func fetchValue<K: FetchKey>(_ key: K) -> Result<K.ValueType, FetchError> {
// The return type here is Result<K.ValueType, FetchError>, but
// now Swift has enough into to infer it!
return Result.success(key.get())
}
// This now works without type inference:
let r0 = fetchValue(GetNameKey())
print(r0)
// And this now (correctly) fails to compile:
// let r1: Result<Double, FetchError> = fetchValue(GetNameKey())
Which approach should you use?
Does each key always return a single, consistent type that you know beforehand (e.g. name is always a string)?
Yes: Use my approach above
No: If a fetch succeeds, but the type that came back isn’t the type the caller asked for, how do you want it reported?
As a Result.failure, and the caller can’t see the value: Use Rob’s approach
As a Result.success, and the caller has to figure out what type the value is: Use Emil’s approach
Prior to Swift 2, I would often use enums with associated values and add functions to extract specific values, like so:
public enum Maybe <T> {
case Unknown
case Known(T)
public var value: T? {
switch self {
case .Unknown: return nil
case .Known(let value): return value
}
}
}
This would allow me to do something like this:
let maybe = .Known("Value")
let val = maybe.value ?? "Another Value"
I would like to get rid of these convenience functions and rely on Swift 2's new syntax. This is possible doing something like:
let val: String
if case .Known(let value) = maybe {
val = value
} else {
val = "Another Value"
}
But I can't figure out how to condense this back into a single line using the ?? operator or even ternary operator.
Is this even possible or am I stuck with defining "extraction" optionals on the enum?
Update (clarification)
The Maybe enum is just an example, but the solution would need to work on Enums that have multiple associated values... like an Either:
public enum Either<L, R> {
case Left(Box<L>)
case Right(Box<R>)
public func left() -> L?{
switch self {
case let Left(value):
return value.value
default:
return nil
}
}
public func right() -> R?{
switch self {
case let Right(value):
return value.value
default:
return nil
}
}
}
The syntax I'm looking for would be something like:
let val = (case .Known(let value) = maybe) ?? "AnotherValue"
What I want to do is easily extract an associated value for a specific case, else provide a default.
For Either it might be something like:
let val = (case .Left(let value) = either) ?? "AnotherValue"
Make sense?
The syntax you want isn't possible in Swift today (and feels unlikely for Swift tomorrow, but I often am surprised). The best available solutions are extraction functions like left() -> L? and right() -> R?. case is not a generic value-returning function that you can extend. See Rob Rix's Either for some of the best current thinking on this problem.
A key choice in Swift is that there are many statements that are not expressions. switch is one of them. Until Swift makes things like switch and if be expressions, it will be very hard to build this kind of syntax IMO.
Just define ?? for it:
func ??<T>(lhs: Maybe<T>, #autoclosure defaultValue: () throws -> T) rethrows -> T {
switch lhs {
case .Unknown: return try defaultValue()
case .Known(let value): return value
}
}
let maybe = Maybe.Known("Value")
let val = maybe ?? "Another Value"
Doing it this way gives us some nice features in Swift2. For instance, we can lazily evaluate the rhs, and we can handle throwing in the rhs:
func computeIt() -> String {
print("LAZY!")
return "Computed"
}
maybe ?? computeIt() // Does not print "LAZY!"
Maybe.Unknown ?? computeIt() // Does print "LAZY!"
enum Error: ErrorType {
case Failure
}
func fail() throws -> String {
throw Error.Failure
}
try maybe ?? fail() // Doesn't throw
do {
try Maybe.Unknown ?? fail() // throws
} catch {
print("THROW")
}
This line of code from "Swift for Beginners" won't work on the latest Swift Version. I'm guessing the repeat keyword can no longer be used as is in the code, so if I change it to repeato, the code loops 5 times as expected but only shows "()" in the console, without the actual String. How can this be fixed?
here is the simple code:
// extension with a closure as a parameter
extension Int {
func repeat(work: () -> ()) {
for _ in 0..<self {
work()
}
}
}
5.repeat({
println("repeat this string")
})
Changes in Swift 2:
repeat is now a keyword
println is now print
This should work:
extension Int {
func times(work: () -> Void) {
for _ in 0..<self {
work()
}
}
}
5.times {
print("repeat this string")
}
func head<T>(xs: [T]) -> T {
if (xs.count > 0) {
return xs.first!
} else {
NSException(name:"empty list", reason:"empty list", userInfo:nil).raise()
}
}
This code does not compile (compiler is expecting every branch to have a return statement and does not recognize NSException as control statement).
How can I change my code to make it compile?
P.S. I don't want to change return value to Optional
Could you reformat your NSException to something that does compile?
Try the answer for this question: Calling NSException.raise() in Swift
I needed fatalError("")
func head<T>(xs: [T]) -> T {
if (xs.count > 0) {
return xs.first!
} else {
fatalError("list is empty")
}
}
this code works just fine.