optional

Avoid isPresent() and get() in control logic

强颜欢笑 提交于 2019-12-22 06:33:11
问题 Is there a prettier way of doing the following in Java 8, avoiding isPresent and get ? void doStuff(String someValue, Optional<Boolean> doIt) { if (doIt.isPresent()) { if (doIt.get()) { trueMethod(someValue); } else { falseMethod(someValue); } } } I tried using map , without success. But I probably didn't try hard enough? 回答1: You can use ifPresent instead of isPresent and get : void doStuff(String someValue, Optional<Boolean> doIt) { doIt.ifPresent (b -> { if (b) trueMethod(someValue); else

When explicitly initializing std::optional's, should I use nullopt? [closed]

别等时光非礼了梦想. 提交于 2019-12-22 05:19:14
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . An std::optional<T> can be initialized to the disengaged state like so: std::optional<int> oi { nullopt }; but also like so: std::optional<int> oi { }; and similarly for assignment ( oi = {} or oi = nullopt ). Other than personal preference / sense of aesthetics, is there a

Is it possible to convert Option<Result<T, E>> to a Result<Option<T>, E> without using match?

自作多情 提交于 2019-12-22 04:17:17
问题 My first thought is to map the Option , but I can't use try! from inside of the closure. The match statement looks unnecessary, but I can't figure out how to simplify it. fn example<T, E>(val: Option<Result<T, E>>) -> Result<Option<T>, E> { Ok(match val { Some(v) => Some(v?), None => None }) } 回答1: You can use Option::map_or(): val.map_or(Ok(None), |v| v.map(Some)) 回答2: In Rust 1.33, transpose() is stable, so you can just call it: fn main() { let x: Result<Option<i32>, ()> = Ok(Some(5)); let

Java Lambda - check if an ArrayList to Stream is empty

流过昼夜 提交于 2019-12-22 04:12:57
问题 I have the following lambda expression and if works fine when bonusScheduleDurationContainers is not empty. If it is empty, I get a NoSuchElementException . How do I check this in the lambda expression? final List<ScheduleDurationContainer> bonusScheduleDurationContainers = scheduleDurationContainersOfWeek.stream() .filter(s -> s.getContainerType() == ScheduleIntervalContainerTypeEnum.BONUS) .collect(Collectors.toList()); final ScheduleDurationContainer bonusScheduleDurationContainer =

Convert optional string to int in Swift

安稳与你 提交于 2019-12-22 04:09:56
问题 I am having troubles while converting optional string to int. println("str_VAR = \(str_VAR)") println(str_VAR.toInt()) Result is str_VAR = Optional(100) nil And i want it to be str_VAR = Optional(100) 100 回答1: You can unwrap it this way: if let yourStr = str_VAR?.toInt() { println("str_VAR = \(yourStr)") //"str_VAR = 100" println(yourStr) //"100" } Refer THIS for more info. When to use “if let”? if let is a special structure in Swift that allows you to check if an Optional holds a value, and

Assign value of Optional to a variable if present

不羁的心 提交于 2019-12-22 03:51:31
问题 Hi I am using Java Optional. I saw that the Optional has a method ifPresent. Instead of doing something like: Optional<MyObject> object = someMethod(); if(object.isPresent()) { String myObjectValue = object.get().getValue(); } I wanted to know how I can use the Optional.ifPresent() to assign the value to a variable. I was trying something like: String myValue = object.ifPresent(getValue()); What do I need the lambda function to be to get the value assigned to that variable? 回答1: You could use

Swift pattern matching with enum and Optional tuple associated values

戏子无情 提交于 2019-12-21 18:32:53
问题 I'm currently using Alamofire and I use an enum to describe the API I used as advised in the readme. The endpoints are represented as follows: public enum API { case GetStops(stopCode:String?) case GetPhysicalStops case GetLinesColors case GetNextDepartures(stopCode:String, departureCode:String?, linesCode:String?, destinationsCode:String?) } The optional parameters are mutually exclusive: public var URLRequest: NSMutableURLRequest { let result:(path:String, parameters:[String:AnyObject]?) =

How to upcast object contained in Java 8 Optional?

别等时光非礼了梦想. 提交于 2019-12-21 10:45:12
问题 Is there an efficient way to perform upcasting while using an Optional object. Here is a sample code: class A{} class B extends A{} B func(){ //do something return new B(); } Optional<B> func2(){ //do something return Optional.of(new B()); } main() { A a = func(); // Upcasting works fine B b = func(); // Upcasting works fine Optional<B> b = func2(); // 1. Works fine Optional<A> a = func2(); // 2. How to make this work ? } (2.) gives an error. I can solve it by creating another function. But

How can we declare Optional Parameters in C#.net? [duplicate]

让人想犯罪 __ 提交于 2019-12-21 07:15:30
问题 This question already has answers here : Can I give a default value to parameters or optional parameters in C# functions? (6 answers) Closed 6 years ago . I am using a method for doing some action, i want the method to be written only once by using Optional Parameters in C#, other than Method Overloading is there any? 回答1: New to visual studio 2010 named and optional arguments for example public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { }

Subscript of a struct doesn't set values when created as an implicitly unwrapped optional

孤人 提交于 2019-12-21 06:19:30
问题 Why can't I change the the "numbers" array using subscripts when "Foo" is an implicitly unwrapped optional? struct Foo { var numbers = [0,0,0] subscript(index: Int) -> Int { get { return self.numbers[index] } set { self.numbers[index] = newValue } } } var fooA:Foo! fooA = Foo() fooA[1] = 1 // does not change numbers array fooA[1] // returns 0 fooA.numbers[1] = 1 // this works fooA[1] // returns 1 var fooB:Foo! fooB = Foo() fooB![1] = 1 // this works fooB![1] // returns 1 For some reason it