optional

Why did guava/java use possible.isPresent() as opposed to Optional.isPresent(possible)?

六月ゝ 毕业季﹏ 提交于 2019-12-11 01:09:09
问题 On https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained it is explained that guava (and later java 8) adds a generic class Optional in order to clear up null checking. If a function returns an Optional it requires the caller to unwrap the string before using it. this is normally done in the form Optional<String> possible = returnAnAbsentOptional(); if(possible.isPresent()){ System.out.println(possible.get()) } If returnAnAbsentOptional returns null, we have a NPE all

Swift flatMap on array with elements are optional has different behavior

妖精的绣舞 提交于 2019-12-11 00:09:07
问题 let arr: [Int?] = [1,2,3,4,nil] let arr1 = arr.flatMap { next in next } // arr1: [1,2,3,4] let arr2: [Int?] = arr.flatMap { next -> Int? in next } // arr2: [Optional(1), Optional(2), Optional(3), Optional(4)] I'm confused by these code, why do they make a difference? update: please see these codes, I let arr: [Int?] = [1,2,3,4,nil] let arr1: [Int?] = arr.flatMap { next in next } // arr1: [Optional(1), Optional(2), Optional(3), Optional(4), nil] let arr2: [Int?] = arr.flatMap { next -> Int? in

Are implicitly unwrapped optionals truly optionals?

此生再无相见时 提交于 2019-12-10 20:59:45
问题 In Swift 4.0, the following code doesn't compile: var str: String! func someFunc(_ s: inout String?) {} someFunc(&str) Now I imagine that str is of type String? in actuality, and the Swift compiler seems to agree: Cannot pass immutable value of type 'String?' as inout argument I can fix this by either changing the the variable to type String? or the function parameters to (_ s: inout String!) but I don't understand why I have to. Swift already seems to agree that var str : String! is "of type

Crash on unwrapping nil optional

丶灬走出姿态 提交于 2019-12-10 18:53:19
问题 I made a new operator which associate a value to a target only if the value is not nil, otherwise does nothing. Basically it's a synthetic sugar for if let foo = foo { faa = foo } : infix operator =? {} func =?<T>(inout lhs: T, rhs: T?) { if let rhs = rhs { lhs = rhs } } func =?<T>(inout lhs: T?, rhs: T?) { if let rhs = rhs { lhs = rhs } } That way I can save some typing: // instead this: if let x = maybeX { z = x } // I can do this: z =? x My issue is that when I get to the line of execution

Why is a Swift implicitly unwrapped optional `nil`?

一世执手 提交于 2019-12-10 18:37:23
问题 self.presentTextInputControllerWithSuggestions(nil, allowedInputMode: WKTextInputMode.Plain) { (results:[AnyObject]!) -> Void in // results can be nil if let speech = results.first as? String { debugPrint(speech) } } Excuse my ignorance, I'm afraid I've missed some basic understanding of optionals. I'm under the impression that ! , the implictly unwrapped optional indicator, is a guarantee that the variable of that type is not nil. Yet this very straightforward Apple API will infrequently

Option as a singleton collection - real life use cases

邮差的信 提交于 2019-12-10 18:25:11
问题 The title pretty much sums it up. Option as a singleton collection can sometimes be confusing, but sometimes it allows for an interesting application. I have one example on top of my head, and would like to learn more of such examples. My only example is running for comprehension on the Option[List[T]] . We can do the following: val v = Some(List(1, 2, 3)) for { list <- v.toList elem <- list } yield elem + 1 Without having Option.toList , it wouldn't be possible to stay in the same for

Swift type inference and type checking issue

依然范特西╮ 提交于 2019-12-10 17:22:12
问题 I'm not looking for an answer like how to do it correctly but why this happens. Here is the code: func isInt(param: AnyObject?) { if let value = param as? Int { print(value) } else { print("Not Int") } if let value = param { if value is Int { print(value) } else { print("Not Int") } } } let a:AnyObject? = 1.2 let b:Float? = 1.2 let c:Double? = 1.2 isInt(a) isInt(b) isInt(c) I understand in the first if loop, the param is casted to Int and then print out 1 . But why in second if loop, if value

How can I map Optional to another Optional if not present? [duplicate]

删除回忆录丶 提交于 2019-12-10 17:14:36
问题 This question already has answers here : Optional orElse Optional in Java (6 answers) Closed 12 months ago . I have this Java 8 code: public Optional<User> getUser(String id) { Optional<User> userFromCache = cache.getUser(id); if (userFromCache.isPresent()) { return userFromCache; } return repository.getUser(id); } It works fine but I'm wondering how can I chain the call to not to use if . I have tried with orElseGet but it doesn't allow to return another Optional<User> but a User . I want

Optional

拈花ヽ惹草 提交于 2019-12-10 17:10:06
Optional 容器类:用于尽量避免空指针异常 Optional.of(T t) : 创建一个 Optional 实例,t是null则报NullPointException Optional.empty() : 创建一个空的 Optional 实例 Optional.ofNullable(T t):若 t 不为 null,创建 Optional 实例,否则创建空实例 isPresent() : 判断是否包含值 orElse(T t) : 如果调用对象包含值,返回该值,否则返回t orElseGet(Supplier s) :如果调用对象包含值,返回该值,否则返回 s 获取的值 map(Function f): 如果有值对其处理,并返回处理后的Optional,否则返回 Optional.empty() flatMap(Function mapper):与 map 类似,要求返回值必须是Optional 来源: oschina 链接: https://my.oschina.net/u/4108547/blog/3141304

Why have multiple version of Optional in Java 8

十年热恋 提交于 2019-12-10 16:59:35
问题 I noticed that, There are multiple versions of many Types in Java 8. For example, The introduced Optional class has many flavors of OptionalInt, OptionalLong etc.. Although the Optional has a type Parameter ( Optional<T> ), we still need some specific types for primitives, Why? I cannot find a BIG difference between the following: Optional<Integer> o = Arrays.asList(1, 3, 6, 5).stream().filter(i -> i % 2 == 0).findAny(); System.out.println(o.orElse(-1)); OptionalInt oi = Arrays.stream(new int