As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [optional]

Optional is an immutable type that may contain a value or reference.

Filter by
Sorted by
Tagged with
-3 votes
1 answer
55 views

std::optional<std::size_t> correct values comparison [closed]

I need to compare two std::optional<std::size_t> variables, considering all possible cases(both std::nullopt or one of ...
Hardwired's user avatar
0 votes
2 answers
69 views

Java spring controller method to trigger e-mail verification procedure [closed]

For readability sake, is it better to use optionals or write my code in an iterative manner? Here's a route I'm working on, does this look readable do you? ...
user9132502's user avatar
5 votes
1 answer
273 views

Filtering an Option Result using a boolean Result in Rust

I'm trying to rewrite one of my pet projects in Rust, and when I was faced with the problem of filtering a Result<Option<T>, E> via a ...
Yalin Gunayer's user avatar
3 votes
1 answer
425 views

Rust idiomatic option handling

Is there a neater way of handling the option response from req.cookie() in this code block (line 3 onwards)? Id like to avoid the nested if statements, and multiple ...
richzilla's user avatar
  • 175
2 votes
3 answers
1k views

Concatenating optional strings in Scala

I have two optional strings, any of them can be None. I want to create their combination with a delimiter between them if they both exist. I expect to be able to come with a more concise and nicer ...
Suma's user avatar
  • 226
2 votes
2 answers
99 views

Treating Optional values

I have a situation when there could be and could not be values in database, which is why i return optional, so the response object that goes back to the UI could be different depending on the ...
Artjom's user avatar
  • 103
-1 votes
1 answer
2k views

Using Java Optional.ofNullable() instead of if/else or ternary conditional [closed]

I've recently reviewed code twice, from two different authors, where the author has cleverly used Thingy z = Optional.ofNullable(x).orElse(y) rather than using, say,...
Kirby's user avatar
  • 115
3 votes
2 answers
84 views

A Start button handler

I was told my code contains a lot of force unwrapping. I thought it's okay to do that if I am sure that the value operated won't be nil: ...
Anatolii Rudenko's user avatar
3 votes
1 answer
284 views

Updates or creates an entity based on if a value is present in an Optional

I need to get car info from a 3rd party web service and persist the data in my application DB. If my DB already has the car, I only update property values that may have changed. Otherwise, I create ...
James's user avatar
  • 299
2 votes
0 answers
54 views

Maybe with a structural pattern matching interface

With a reference implementation for PEP 622 now available in a Jupyter playground, I decided to copy over my favourite feature of Rust in full to Python. Note: this won't be released until Python 3.10 ...
Peilonrayz's user avatar
  • 42.3k
3 votes
1 answer
264 views

Unwrapping optionals for max of a 2D array in swift

I'm working in swift and I've got a custom class Array2D to handle fixed-width, two-dimensional arrays of integers. What I'm trying to do is write a simple, ideally-...
Zeph's user avatar
  • 133
4 votes
1 answer
2k views

C++ Optional Reference

Here is a good article on an optional reference type in C++. They discuss std::optional<T&>, but as that doesn't compile I have made my own. One purpose ...
Blue7's user avatar
  • 373
2 votes
1 answer
6k views

Best practice to initialize instance variables for a POJO when values are null

I am using Java 8. I haven't found another question that fits mine exactly, and I've come across some conflicting information on best practices with what I'm trying to do. I'm creating a POJO which ...
user355381's user avatar
7 votes
3 answers
720 views

Coalescing lvalue references to std-optionals

I want to coalesce (lvalue references to) optionals of the same type, in C++17: ...
einpoklum's user avatar
  • 1,985
1 vote
1 answer
137 views

Is there better alternative to making an optional set of elements from iterable in java?

I'm feeling uneasy with this service method that I wrote: ...
Mohammed Housseyn Taleb's user avatar
6 votes
2 answers
5k views

Is it worth to use if-else statement as Java optional pattern?

I came to a point while refactoring using if {} else {} code with Java optionals. While optimizing code it turned to a result to this: ...
Daniel Gschösser's user avatar
4 votes
1 answer
96 views

Force-unwrapped property as a contract

I have a class, which is meant to show a full screen modal controller with a set of actions (apart from actions it also has some static content, a lot of labels and images which are irrelevant, so I ...
The Dreams Wind's user avatar
2 votes
1 answer
3k views

`std::optional` under C++14 v1

This is a first version of an implementation of std::optional it is supposed to compile under C++14. The public interface of the class is complete, but there are ...
Harald Scheirich's user avatar
3 votes
1 answer
76 views

Reference service for a cache

I'm using JDK 11. This is my code: ...
Jordi's user avatar
  • 133
-3 votes
1 answer
38 views

Change Kotlin optional-like style [closed]

I have next method in client fun authUser(loginRequest: LoginRequest): String? I need to implement next logic: check if user already exists - nothing to do, if ...
Artyom Karnov's user avatar
7 votes
2 answers
3k views

C++ Maybe<T> implementation

In order to improve my understanding of C++ template meta-programming, SFINAE, references, and overall class design, I've tried to implement a Maybe<T> class ...
A P's user avatar
  • 173
2 votes
1 answer
145 views

Haskell/Yesod - Butchering use of MaybeT

The following is some code that is used to provide the frontend with certain parameters when rendering a form. The parameters are conditional based around what data is being changed, ...
danbroooks's user avatar
3 votes
2 answers
94 views

Conditionally display an Action depending on whether or not a player is connected

Is there a nicer way to write the maybeWhen portion of displayConnectionAction ? I want to conditionally display an ...
danbroooks's user avatar
7 votes
2 answers
140 views

Class template for the encapsulation of datasheet specifications using optionals

A couple of years ago I wrote a pair of class templates to encapsulate specifications which have one or more of a minimum, typical, and/or maximum value, e.g. on a datasheet like this one for the 741 ...
Null's user avatar
  • 1,433
3 votes
1 answer
87 views

Java builder for user contact information, with optionals

I have this simple builder here: ...
baant's user avatar
  • 31
2 votes
1 answer
475 views

Fed Singleton struct class from plist

I have a plist file which has URLs in it and struct class is feeding from this plist. This struct class has static variable which I use to reach URLs in every part of the app. My question is that I'm ...
Mr Some Dev.'s user avatar
2 votes
1 answer
42 views

Append /src to an Optional path

My method getProjectDir() will return an Optional which contains the path to the project directory, or an empty container: ...
Hai Vu's user avatar
  • 650
1 vote
1 answer
72 views

Handling multiple IO operations that return optional values

The following Haskell code reads a Pandoc document from the DB and then serves it to the user after making a transformation, after the first run it is stored in memory via ...
danbroooks's user avatar
8 votes
1 answer
4k views

Optional<T> implementation

I wrote an Optional<T> implementation in C++14 in order to support pre-C++17 compiler. Differences to ...
user avatar
3 votes
1 answer
140 views

Grab first successful pair from a list of maps

I have a function that parses geographic coordinates through a list of different providers. In this case I want to look up two addresses. The main problem of this question is - given a List of of ...
stan's user avatar
  • 209
3 votes
2 answers
142 views

Confirming whether a user is registered, using Optional class in Java 8

I start to learn Java 8 and I wondering is that kind of using Optional class is correct? ...
Mateusz Sobczak's user avatar
5 votes
0 answers
222 views

deep_ptr<T>; a header-only, deep copying, value semantic smart pointer for optionally defined types

Edit: final revision here A couple days ago I posted a similar question here. Since then, I have refined the implementation a bit further, as the solution I had previously posited was a bit off ...
Tom's user avatar
  • 405
6 votes
1 answer
197 views

Nullable<T>; header-only value semantics for forward declared types

Edit: I have refined the concept a bit further, and my new implementation can be found here Motivation: I found myself with class members of forward declared types in a header, such as: ...
Tom's user avatar
  • 405
5 votes
1 answer
491 views

C++ Nullable template class

Today I implemented a C++11 template class which allows for Nullable types. The reason for this is that std::optional is not yet available, (I use C++11/14) and I wanted to practice a bit, so I ...
Hindrik Stegenga's user avatar
7 votes
3 answers
2k views

Variant optional to return error string or value

I needed a class with more information than optionals give, I need to know why the optional is not valid in error states. I used a Boost variant for this. Since this will be wrapping almost all of my ...
Stephen Eckels's user avatar
10 votes
1 answer
243 views

Implementing a Maybe for multidimensional data

The scenario is we're working with a REST endpoint that gives us nice JSON objects. We're using requests, and everything works wonderfully. But one day you notice that data you though was always being ...
Peilonrayz's user avatar
  • 42.3k
5 votes
3 answers
80 views

Concatenating multiple feedback messages

The goal is to build a String from a given set where Feedback is an interface and ...
imTachu's user avatar
  • 153
12 votes
1 answer
829 views

An optional_ref<T>

I omitted all free operators but the equality comparisons ones because of verbosity. I am glad about any comments and improvements. Motivation I know that optional references are equivalent to ...
Maikel's user avatar
  • 740
3 votes
2 answers
213 views

Use optional to check Null pointer exception

I currently have something like this: ...
OPK's user avatar
  • 285
1 vote
3 answers
214 views

Select a list from a 2D list, take the last item from its nearest non empty left neighbour and put it at its head

I've just got into Haskell a few days ago and I love it. I'm looking for some pointers and best practices on how to organise and write Haskell code specifically when it comes to managing errors, which ...
iluvAS's user avatar
  • 113
3 votes
1 answer
373 views

Maybe type implementation

This supposed to implement a Maybe type that can either hold a value T or "nothing". It's designed to work well with vectors (...
bitmask's user avatar
  • 175
23 votes
3 answers
4k views

Seamlessly migrating <experimental/optional> to <optional>

From this Stack Overflow answer, I learned that C++17 will have __has_include, which can [make] migrating from experimental to std almost seamless This still ...
Justin's user avatar
  • 3,265
21 votes
2 answers
17k views

Filtering out empty Optionals

I want to convert my list of optional strings to a list of strings by getting rid of the empty Optionals. Is there a shorter version for achieving this than the ...
Matthias Braun's user avatar
3 votes
3 answers
4k views

Optional Consumer for ifNotPresent

As Optional have only ifPresent and not have another function to be run in false case so I did the below class for that, please check it and give me your feedback: ...
Bassem Reda Zohdy's user avatar
2 votes
2 answers
5k views

C++ Optional Implementation

I haven't done much programming in C++, so I figured I'd try making an Optional class. I'm mainly concerned that I didn't follow good conventions or that I didn't ...
Justin's user avatar
  • 3,265
8 votes
1 answer
2k views

An attempt at implementing Maybe in C++11

I gave a shot at implementing Maybe for C++ and a slight twist in my implementation is that it uses thread_local static instance ...
Vite Falcon's user avatar
3 votes
1 answer
415 views

Simple/Naive Implementation of Identity and Maybe Monad in Elixir

This is my first crack at a identity monad and a maybe monad in Elixir: ...
Onorio Catenacci's user avatar
3 votes
2 answers
343 views

Adding two values in a map, if they exist

My code looks like imperative style code. I want to make my code more functional. How I can rewrite my program so it become functional style code? ...
MyTitle's user avatar
  • 265