Questions tagged [optional]

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

Filter by
Sorted by
Tagged with
2
votes
1answer
77 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 ...
3
votes
3answers
67 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 ...
2
votes
2answers
63 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 ...
-1
votes
1answer
93 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,...
3
votes
2answers
65 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: ...
3
votes
1answer
75 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 ...
2
votes
0answers
26 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 ...
3
votes
1answer
59 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-...
3
votes
1answer
142 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 ...
1
vote
1answer
530 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 ...
7
votes
3answers
588 views

Coalescing lvalue references to std-optionals

I want to coalesce (lvalue references to) optionals of the same type, in C++17: ...
1
vote
1answer
58 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: ...
5
votes
2answers
1k 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: ...
4
votes
1answer
85 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 ...
2
votes
1answer
977 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 ...
3
votes
1answer
70 views

Reference service for a cache

I'm using JDK 11. This is my code: ...
-3
votes
1answer
36 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 ...
6
votes
2answers
649 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 ...
2
votes
1answer
111 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, ...
3
votes
2answers
85 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 ...
4
votes
0answers
72 views

Functional implementation of the Maybe Monad in JS [closed]

A while ago, I really wanted to have the Maybe Monad in my JS projects. I ended up trying to use Promises as a stand-in solution (using Promise.resolve as my unit, ...
7
votes
2answers
133 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 ...
3
votes
1answer
82 views

Java builder for user contact information, with optionals

I have this simple builder here: ...
2
votes
1answer
444 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 ...
2
votes
1answer
35 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: ...
1
vote
1answer
60 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 ...
6
votes
1answer
2k views

Optional<T> implementation

I wrote an Optional<T> implementation in C++14 in order to support pre-C++17 compiler. Differences to ...
3
votes
1answer
137 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 ...
3
votes
2answers
139 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? ...
4
votes
0answers
187 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 ...
6
votes
1answer
179 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: ...
5
votes
1answer
284 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 ...
4
votes
1answer
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 ...
10
votes
1answer
235 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 ...
5
votes
3answers
75 views

Concatenating multiple feedback messages

The goal is to build a String from a given set where Feedback is an interface and ...
11
votes
1answer
433 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 ...
3
votes
2answers
202 views

Use optional to check Null pointer exception

I currently have something like this: ...
1
vote
3answers
109 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 ...
3
votes
1answer
308 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 (...
23
votes
3answers
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 ...
21
votes
2answers
15k 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 ...
3
votes
3answers
3k 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: ...
2
votes
2answers
4k 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 ...
8
votes
1answer
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 ...
3
votes
1answer
355 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: ...
3
votes
2answers
332 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? ...