optional

Why use Optional.of over Optional.ofNullable?

假如想象 提交于 2019-12-17 05:33:07
问题 When using the Java 8 Optional class, there are two ways in which a value can be wrapped in an optional. String foobar = <value or null>; Optional.of(foobar); // May throw NullPointerException Optional.ofNullable(foobar); // Safe from NullPointerException I understand Optional.ofNullable is the only safe way of using Optional , but why does Optional.of exist at all? Why not just use Optional.ofNullable and be on the safe side at all times? 回答1: Your question is based on assumption that the

Why java.util.Optional is not Serializable, how to serialize the object with such fields

烈酒焚心 提交于 2019-12-17 03:53:14
问题 The Enum class is Serializable so there is no problem to serialize object with enums. The other case is where class has fields of java.util.Optional class. In this case the following exception is thrown: java.io.NotSerializableException: java.util.Optional How to deal with such classes, how to serialize them? Is it possible to send such objects to Remote EJB or through RMI? This is the example: import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream

Swift optional property using KVC causes crash

不问归期 提交于 2019-12-14 03:36:40
问题 I found that using KVC in Swift causes many problems, especially with optional properties. Here is my specific problem: Here is a Class named Person . It has a normal property called age ,and a Optional(Int) property called ageOptional . class Person: NSObject { var age: Int var ageOptional: Int? override init(age: Int){ self.age = 0 } } Now, I use KVC in Person's instance: //new a instance var person = Person() //kvc for normal property: it work well person.setValue(28, forKeyPath: "age") /

Type inference fails when using nil-coalescing operator with two optionals

主宰稳场 提交于 2019-12-14 00:32:18
问题 We are trying to figure whether this is a bug in Swift or us misusing generics, optionals, type inference and/or nil coalescing operator. Our framework contains some code for parsing dictionaries into models and we've hit a problem with optional properties with default values. We have a protocol SomeProtocol and two generic functions defined in a protocol extension: mapped<T>(...) -> T? mapped<T : SomeProtocol>(...) -> T? Our structs and classes adhere to this protocol and then parse their

swift How to determine if a variable is an optional

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 22:48:46
问题 I want to know if a variable is an optional I try method below, but fail func isOptional(v: Any) -> Bool { return v is Optional } 回答1: As an academic exercise (can it be done vs. should it be done), I came up with this: func isOptional(a: Any) -> Bool { return "\(a.dynamicType)".hasPrefix("Swift.Optional") } Example: let name = "Fred" let oname: String? = "Jones" let age = 37 let oage: Int? = 38 let arr: [Any] = [name, oname, age, oage] for item in arr { println("\(item) \(isOptional(item))")

Make Java infer @NotNull for Optional Return Types

徘徊边缘 提交于 2019-12-13 17:02:48
问题 Using Optional s in Java does not protect from NPEs since values still can be null : Optional<String> myMethod() { return null; } A way to protect from this at least at runtime is the use of @NotNull annotations. (Not possible at compile time in Java since it does not have non-null references, as opposed to more sophisticated languages like Swift and Kotlin). Using @NotNull will immediately crash when null is detected, thus preventing null traversing through the program, and making it easier

Idiomatic way to test Swift Optionals

只愿长相守 提交于 2019-12-13 16:23:40
问题 I'm NOT asking if these extensions are a good idea or not, it's just a thought experiment, I'm trying to learn from the practice. Agreeing with Christian Lattner, that methods are generally preferable, I thought I'd play with being able to express: someVariable.isNil and someVariable.notNil Implementing it, I found myself curious if one or the other of the following implementations was preferable to the other, and for what reasons? Would one be more efficient than the others. Would there be

Jackson deserializing Optional throws NoSuchFieldError

落花浮王杯 提交于 2019-12-13 14:37:24
问题 I developing a Spring Boot application and I tried to include an Optional<String> ( java.lang ) field in one of my models, so If the @RestController I am using with won't get such a field, it will include it as an Optional.empty() . Every time I call the method via POST I get a java.lang.NoSuchFieldError: _valueInstantiator exception. I started experimenting and made a stripped down version of the deserializer, that still gives me the error: The model: import com.fasterxml.jackson.annotation

Swift: Nested optionals in a single guard statement

感情迁移 提交于 2019-12-13 13:43:02
问题 I am trying to guard a conversion from string to Float to Int: guard let v = Int (Float("x")) else { return -1 } The swift 3 compiler complains: value of optional type 'Float?' not unwrapped; did you mean to use '!' or '?'? Adding "?" does not help, though. And "!" would be wrong here, wouldn't it? Is it possible to solve this, without having to use two lines or two guard statements? 回答1: Optional has a map function made just for this: guard let v = Float("x").map(Int.init) else { return nil

Parse JSON with optional field

放肆的年华 提交于 2019-12-13 12:22:09
问题 I'm trying to parse in android studio a JSON, that containts this : "stops": [ { "num": 1, "time": "2016-04-27T06:15:00.000Z", "title":"Flight to London", "desc":"Barcelona BCN-London-Gatwick LGW", "type":"0", "subtype":0 }, { "num": 2, "time": "2016-04-27T10:35:00.000Z", "title":"Gatwick express", "desc":"From Airport to London", "type":"0", "subtype":1 }, { "num": 3, "time": "2016-04-27T12:15:00.000Z", "title":"Pub the black horse", "desc":"From Airport to London", "type":1, "subtype":1,