Regex optional group
Making a non-capturing, zero to more matching group, you must append ?. (?: ….. )? ^ ^____ optional |____ group
Making a non-capturing, zero to more matching group, you must append ?. (?: ….. )? ^ ^____ optional |____ group
Not in the language, no, but you can make your own: public struct Optional<T> { public bool HasValue { get; private set; } private T value; public T Value { get { if (HasValue) return value; else throw new InvalidOperationException(); } } public Optional(T value) { this.value = value; HasValue = true; } public static … Read more
As you’re using the identity transform { $0 }, the compiler will infer that ElementOfResult? (the result of the transform) is equivalent to Element (the argument of the transform). In this case, Element is String?, therefore ElementOfResult? == String?. There’s no need for optional promotion here, so ElementOfResult can be inferred to be String. Therefore … Read more
Java 9 and above: firstOptional.or(() -> secondOptional); Java 8 and below If you want to avoid mentioning firstOptional twice, you’d probably have to go with something like firstOptional.map(Optional::of).orElse(secondOptional); or Optional.ofNullable(firstOptional.orElse(secondOptional.orElse(null))); But the most readable variant is probably to simply do Optional<…> opt = firstOptional.isPresent() ? firstOptional : secondOptional.isPresent() ? secondOptional : Optional.empty(); If someone stumbles … Read more
Update Apple has now added a coalescing operator: var unwrappedValue = optionalValue ?? defaultValue The ternary operator is your friend in this case var unwrappedValue = optionalValue ? optionalValue! : defaultValue You could also provide your own extension for the Optional enum: extension Optional { func or(defaultValue: T) -> T { switch(self) { case .None: … Read more
What use case is there for iterating over an Option? My favorite reason, in a word, is flatten: fn main() { let results = [Some(1), None, Some(3), None]; let sum: i32 = results.into_iter().flatten().sum(); println!(“{}”, sum) } Before Rust 1.29, you can use flat_map: fn main() { let results = vec![Some(1), None, Some(3), None]; let sum: … Read more
Optional chaining wraps whatever the result of the right side is inside an optional. So if run() returned T, then x?.run() returns T?. Since run() returns Void (a.k.a. ()), that means the whole optional chaining expression has type Void? (or ()?). When a closure has only one line, the contents of that line is implicitly … Read more
The following does initialise x to nil, the brackets are entirely superfluous. var x: UIView? return x == nil Will return true Check out the developer docs for more information. https://developer.apple.com/library/content//documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html If you define an optional variable without providing a default value, the variable is automatically set to nil for you: var surveyAnswer: String? // … Read more
I think I am starting to understand your question. Execution order with Optional can be different from what we are used to in procedural programming (the same is true for Java streams and other code using lambdas). I will use the two examples from Eugene’s answer: o1.orElse(new MyObject()); // 1055e4af This is plain old Java: … Read more
Great news. Unwrapping multiple optionals in a single line is now supported in Swift 1.2 (XCode 6.3 beta, released 2/9/15). No more tuple/switch pattern matching needed. It’s actually very close to your original suggested syntax (thanks for listening, Apple!) if let email = emailField?.text, password = passwordField?.text { } Another nice thing is you can … Read more