optional

Calling method after Optional.filter() without passing in object

99封情书 提交于 2019-12-08 03:24:14
问题 I can understand the below: user .filter(u -> "Sam".equals(u.getName())) .ifPresent(this::doSomethingWithUser); if user.getName is equal to "Sam" then pass user to local method doSomethingWithUser() . However, what if I simply wanted to call a local method, without passing user but keeping the check that user.getName is equal to "Sam" ? Does this use case make using Optional redundant? 回答1: Just ignore the mandatory function parameter. This means you cannot use shorthand :: syntax and goes

Calling method after Optional.filter() without passing in object

心已入冬 提交于 2019-12-07 23:14:30
I can understand the below: user .filter(u -> "Sam".equals(u.getName())) .ifPresent(this::doSomethingWithUser); if user.getName is equal to "Sam" then pass user to local method doSomethingWithUser() . However, what if I simply wanted to call a local method, without passing user but keeping the check that user.getName is equal to "Sam" ? Does this use case make using Optional redundant? Just ignore the mandatory function parameter. This means you cannot use shorthand :: syntax and goes like this: user .filter(u -> "Sam".equals(u.getName())) .ifPresent(u -> doSomethingWithoutUser()); You can

Optional include in PHP

南楼画角 提交于 2019-12-07 22:49:46
问题 I have a config file with the general configuration (in a git repo), and a local config file that overwrites configuration properties (ignored in the repo). Right now the local config file is included at the beginning of the config file: include_once 'local_config.php'; But I would like the include to be conditional: only do it if the file local_config.php actually exists. I can do a enter link description here without problems, but first I would need to check if the file exists. So I tried

Make a dictionary value non-optional as extension

喜欢而已 提交于 2019-12-07 21:50:47
问题 The below playground outlines my issue. The extension will remove nil values from my dictionary, but leave the other values as Optional(Value). What I need is a dictionary that has no nil values and make the optional value type non-optional. Ex: I have a dictionary of [String:Int?] . I want the jsonSantize() of that called on that dictionary to return a [String:Int] . //: Playground - noun: a place where people can play import UIKit import Foundation protocol OptionalType { associatedtype

Is adapting a no-arg method into a Consumer bad form?

蓝咒 提交于 2019-12-07 18:48:48
问题 Someone raised a question on another SO Answer about whether it is bad practice or inefficient to do this: Optional<User> user = ... user.ifPresent(u -> doSomethingWithoutUser()); instead of if (user.isPresent()) doSomethingWithoutUser(); Specifically, the fact that we're adapting a zero-arg method into a Consumer<User> which ignores its parameter u . As this isn't a Stream non-terminal operation, the fact doSomethingWithoutUser() likely has side-effects isn't a concern. I'm not bothered

What's the difference between Optional<T> and optional types in Swift? Extending Optional to carry error information?

Deadly 提交于 2019-12-07 18:48:45
问题 Update - there is no difference between Optional and optional types in Swift - they are the same thing. So in Swift they introduced the Type? construct, that creates an optional type which "forces" the programmer to check if the value actually exists. Also, in Swift there is no exception handling. But there is this built-in optionality mechanism. This optional feature ? is just an Optional<T> enum behind the scenes inspired from Haskell's Maybe . I was wondering if there was any way of

Early return/golden path in Swift

╄→гoц情女王★ 提交于 2019-12-07 11:14:48
问题 I'm used to write code with early return/golden path in Objective-C. I tried this approach in Swift, and noticed that early return comes at the expense of using the forced unwrapping operator ( ! ) when optionals are involved. Take a method that calculates the size of a directory. First, the golden path version: private func calculateSize_GoldenPath(directory:String) -> UInt64 { let fileManager = NSFileManager.defaultManager() var error : NSError? var contents = fileManager

I need to use a Java 8 Optional method that either gets the wrapped value, or calls a void return Consumer lambda

好久不见. 提交于 2019-12-07 04:06:50
问题 I am new to using optionals in Java 8. I know the method orElseGet() takes a supplier and orElseThrow() also takes a supplier that throws an exception. orElseThrow() might be a good one to use if I can construct my own exception and do something when that exception is triggered. My main goal is to use a method that will either get the unwrapped value, or if the optional wraps null, then to actually execute an entirely different function. Looking for the closest thing to: class DoInsteadClass

experimental::optional nullopt_t constructor

蓝咒 提交于 2019-12-07 03:27:13
问题 Here is described the nullopt_t and nullopt for the optional object proposed for c++: struct nullopt_t{see below}; constexpr nullopt_t nullopt(unspecified); [...] Type nullopt_t shall not have a default constructor. It shall be a literal type. Constant nullopt shall be initialized with an argument of literal type. The reason for this is explained in the The op = {} syntax chapter of the document: for the op = {} to be unambiguous some tricks have to be adopted, one of which is that nullopt_t

Optional<> and return type narrowing

亡梦爱人 提交于 2019-12-07 01:10:22
问题 In Java < 8, returning "unsafe" objects (objects or null), I was able to specialize return type in subclass: class A {} class B extends A {} interface Sup { A a(); /* returns A instance, or null */ } interface Sub extends Sup { B a(); } In Java 8, if I want to make my API "safer", I should return Optional<A> instead of "raw" A : interface Sup { Optional<A> a(); } interface Sub extends Sup { Optional<B> a(); } But doesn't compile! Because Optional<B> is not a subclass of Optional<A> . How I'm