optional

JSON: Serialize Guava Optional

ぃ、小莉子 提交于 2020-01-01 12:31:09
问题 Is there a Json Serializer/Deserializer for com.google.common.base.Optional? Out of the box this doesn't seem to work with Jackson, see below: package com.example; import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import com.google.common.base.Optional; public class TestClass { public Optional<String> myString; public TestClass() { myString = Optional.of(

Protocols: Why is @ObjC required for conformance checking and optional requirements?

為{幸葍}努か 提交于 2020-01-01 08:49:34
问题 The Swift documentation says the following about protocols : You can check for protocol conformance only if your protocol is marked with the @objc attribute, as seen for the HasArea protocol above. This attribute indicates that the protocol should be exposed to Objective-C code and is described in Using Swift with Cocoa and Objective-C. Even if you are not interoperating with Objective-C, you need to mark your protocols with the @objc attribute if you want to be able to check for protocol

How to peek on an Optional?

纵饮孤独 提交于 2020-01-01 07:52:29
问题 I want to use the fluent api of Optional and apply two Consumer s to it. I'm dreaming about something like this: Optional.ofNullable(key) .map(Person::get) .ifPresent(this::printName) .ifPresent(this::printAddress); // not compiling, because ifPresent is void How do I apply several Consumer s to an Optional ? 回答1: Here's how you can implement the missing peek method for Optional : <T> UnaryOperator<T> peek(Consumer<T> c) { return x -> { c.accept(x); return x; }; } Usage: Optional.ofNullable

Swift Lazy and Optional properties

大憨熊 提交于 2020-01-01 05:45:14
问题 What is the difference between a Lazy or Optional property in Swift? For example, if someone is building a navigation bar that comes in from the side, I think that should all be within one UIViewController . The user might never open the menu but sometimes they will. var menu: NavigationBar? lazy var menu: NavigationBar = NavigationBar.initialize() Both of the optional I think are good code, because they don't create the view unless its needed. I understand Optional means there might be a

Usage of where in if let assignment in Swift

爷,独闯天下 提交于 2019-12-31 08:34:27
问题 The Swift documentation at page 61 of the Swift manual hints to the possibility of using where to join an optional binding with a regular condition. Yet when I do it I have a warning suggesting me to substitute the where with a comma like in the following piece of code: if let geocodingError = error as? NSError where geocodingError.code == 2 回答1: Example with two conditions if let x = y, let a = b, a == x && !x.isEmpty { 回答2: In Swift 3 this syntax has changed. What was if let x = y, a = b

Incrementing an implicitly unwrapped optional

早过忘川 提交于 2019-12-31 03:26:10
问题 I declare an implicitly unwrapped optional as: var numberOfRows: Int! and initialize it in init: numberOfRows = 25 Later I need to decrement it by one so I write: numberOfRows-- but this doesn't compile. The error message says the decrement operator can't be applied to an implicitly unwrapped optional. With a little experimentation I find that the following compiles without error: numberOfRows!-- I would like to understand this. What is the explanation for what seems like the extra '!'? 回答1:

jdk8的特性stream().map()将对象转换成另一个对象

橙三吉。 提交于 2019-12-30 12:24:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> java8的optional的使用: http://www.jdon.com/idea/java/using-optional-effectively-in-java-8.html http://www.runoob.com/java/java8-optional-class.html Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象。 Optional 是个容器:它可以保存类型T的值,或者仅仅保存null。Optional提供很多有用的方法,这样我们就不用显式进行空值检测。 Optional 类的引入很好的解决空指针异常。 在Java 8中 stream().map() ,您可以将对象转换为其他对象。查看以下示例: 1.大写字符串列表 1.1简单的Java示例将Strings列表转换为大写。 TestJava8.java package com.mkyong.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class

Swift: Compiler's conversion from type to optional type

╄→尐↘猪︶ㄣ 提交于 2019-12-30 11:54:32
问题 It looks like compiler automatically converts a type into an optional type when needed, even though there is no inheritance relationship here. Where in the documentation is this behavior specified? func test(value: String?) { // String passed in is now an optional String instead. print(value ?? "") } // Pass an actual string test(value: "test") 回答1: This behaviour is actually explicitly documented in a well-hidden corner of the docs folder of the Swift github repo. Citing swift/docs/archive

Deserialization of optional fields from BinaryFormatter

二次信任 提交于 2019-12-30 10:08:10
问题 I have an application that serializes data using BinaryFormatter . A member was added to the class that was serialized from one version to the next without changing the class name. Code was added to handle the possible absence of the added member in old serialized files: private void readData(FileStream fs, SymmetricAlgorithm dataKey) { CryptoStream cs = null; try { cs = new CryptoStream(fs, dataKey.CreateDecryptor(), CryptoStreamMode.Read); BinaryFormatter bf = new BinaryFormatter(); string

How to set optional parameter without compile-time constant

泄露秘密 提交于 2019-12-30 07:57:13
问题 Is there a way to write the C# method below: public string Download(Encoding contentEncoding = null) { defaultEncoding = contentEncoding ?? Encoding.UTF8; // codes... } with a default parameter added so it looks like this: public string Download(Encoding contentEncoding = Encoding.UTF8) { // codes... } without using a compile-time constant? 回答1: In short. No. Optional parameters are required to be compile time constants or value types. From Named and Optional Arguments (C# Programming Guide)