Questions tagged [unwrap]

181 questions
46
votes
1 answer

Why choosing `unwrap_or_else` over `unwrap_or`?

fn main() { let _one = None.unwrap_or("one".to_string()); let _two = None.unwrap_or_else(|| "two".to_string()); } Any particular reason why people should prefer unwrap_or_else over unwrap_or? I have seen comments that unwrap_or is eager…
0x00A5
  • 1,462
  • 1
  • 16
  • 20
18
votes
4 answers

Map with Optional Unwrapping in Swift

Say I had the below api : func paths() -> [String?] { return ["test", nil, "Two"] } And I was using this in a method where I needed [String], hence I had to unwrap it using the simple map function. I'm currently doing : func cleanPaths() ->…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
10
votes
1 answer

Does Kotlin have an equivalent to Implicitly Unwrapped Optionals in Swift?

Implicitly unwrapped optionals are a useful feature of Swift for things like UI elements that are not assigned during a class's constructor, but can be safely be assumed to be non-null for the majority of functions (as they will have been assigned…
stantronic
  • 397
  • 7
  • 14
10
votes
2 answers

"Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?' ?"

I have not been studying iOS or Swift for very long. With one of the latest Xcode updates, many of the apps I have made on my computer now appear to be using obsolete syntax. Xcode talks us through converting it to new syntax but often that does not…
HeeHeeHaha
  • 139
  • 1
  • 1
  • 8
8
votes
2 answers

Alternative to `unwrap()` when `T` does not implement `Debug`

I am aware that x.unwrap() when x: Result does not work when E does not implement Debug: unwrap() would need to print out the Err variant in case x.is_err() but it cannot. Sometimes, however, especially in tests, I do need to get my hands on…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
7
votes
1 answer

Is there a way to detect forced unwrapping across a Swift project?

Is there a way (via a compiler flag or a script) to detect forced unwraps across a Swift project? I'm thinking about stuff like these: let b = a as! B let c = a! a!.method() Without triggering false-positives for var a: A! for instance.
ldiqual
  • 15,015
  • 6
  • 52
  • 90
7
votes
1 answer

Scala: Access optional value in optional object

Is there a good way to access a Option Value inside a Option Object? The nested match cases result in a ugly tree structure. So if I have for example: case class MyObject(value: Option[Int]) val optionObject : Option[MyObject] =…
lukaswelte
  • 2,951
  • 1
  • 23
  • 45
6
votes
2 answers

Checking if `if let` is nil

I have an app where I'm currently using the SwiftKeychainWrapper. Below is the code I have which checks if retrievedString is nil. However I'm still getting retrievedString: nil in the console. Shouldn't the code in the if-let statement not run, or…
C. Wagner
  • 461
  • 7
  • 13
5
votes
2 answers

Function throws AND returns optional.. possible to conditionally unwrap in one line?

I am using an SQLite library in which queries return optional values as well as can throw errors. I would like to conditionally unwrap the value, or receive nil if it returns an error. I'm not totally sure how to word this, this code will explain,…
twiz_
  • 1,178
  • 10
  • 16
5
votes
1 answer

Numpy unwrap ignoring NaN

I have a long numpy array with wind direction records, and I'm trying to use numpy's unwrap before running an algorithm to detect jumps in the data. The data contains NaNs, and numpy seems unable to process this. As soon as one NaN is encountered,…
Peter9192
  • 2,899
  • 4
  • 15
  • 24
4
votes
2 answers

Is there a way to recursively unwrap a function type in TypeScript?

Let's say we have the following type I: type I = () => () => () => "a" | "b" | "c"; Is there a way to create a generic type Unwrap such that Unwrap evaluates to "a" | "b" | "c"? type I = () => () => () => "a" | "b" | "c"; type Result =…
4
votes
1 answer

How to unwrap_or to String

When using unwrap_or, how do I get a String? I've distilled my problem down to this (with more type annotation than needed): fn main() { let mut blah: String; let opt: Option<&str> = Some("foo"); blah = opt.unwrap_or("bar"); } This…
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
4
votes
5 answers

swift - Remove white spaces from var(UITextField input) doesn't work

I'm new to swift, but from ObjectiveC background, I tried to get an input from textfield into a var, on a button click. Now, when I tried to remove blank space using "stringByTrimmingCharactersInSet" and so many other options, it's not working. Here…
Rajesh S
  • 133
  • 1
  • 9
4
votes
1 answer

Unwrap a tuple in swift

This seems valid code to me but it doesn't unwrap the tuple func updateUserDefaults(data:T) { if let data = data as? (String, Any) { println(data.1) } } updateUserDefaults(("loop", true)) my goal is to make this a generic setter…
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
4
votes
1 answer

Can this unwrap/pattern matching code be made more clear/idiomatic?

I am exploring different ways of implementing a linked list in Rust as a learning project. In one particular place, I've got some code that works properly, but it makes multiple calls to unwrap--I am under the impression this is generally regarded…
GrandOpener
  • 1,943
  • 1
  • 17
  • 25
1
2 3
12 13