Questions tagged [option-type]
The option type or maybe type is a polymorphic type that represents encapsulation of an optional value where one side encapsulates the value, and other side represents the absence of the value (None, Nothing, etc). This tag is adequate for questions about `Option` in Scala, `Optional` in Java and Swift, `std::optional` in C++ and similar types.
option-type
3,818
questions
-2
votes
0
answers
11
views
The automatic selection method of two drop-down fields linked together with JavaScript [closed]
When the site is reloaded, the drop-down boxes will be filled automatically
And after the first drop-down field is filled, the second drop-down field will be filled automatically
For example, the ...
-2
votes
0
answers
37
views
Why Java shows strange characters when print a OptionalDouble? [duplicate]
I declared a OptionalDouble vector, and when the program print this give me strange characters instead of the vector values:
[[Ljava.util.OptionalDouble;@37bba4006. [[Ljava.util.OptionalDouble;@...
0
votes
2
answers
59
views
unwrap or evalulate to false in rust
I have encountered several cases similar this:
if let Some(x) = formula_chars.peek() {
if x.is_lowercase() {
todo!()
}
}
where I would like an expression to evaluate to false if an ...
-4
votes
0
answers
75
views
std::optional is not available though I'm using G++9 [closed]
`error: ‘optional’ in namespace ‘std’ does not name a template type
** | std::optional<double> ;
| ^~~~~~~~
‘std::optional’ is only available from C++17 onwards
** | std::...
-4
votes
2
answers
250
views
Why `emplace()` without arguments returns 0 when I try to read an `std::optional<unsigned>` variable? [duplicate]
Below is the example code snippet:
#include <optional>
#include <iostream>
int main()
{
std::optional<unsigned> check = 200;
std::cout << check << std::endl;
...
1
vote
8
answers
138
views
Decent idiom for initializing an optional to either null or a value?
Motivation
With C++ having gotten optional's (in C++17), it is now common to want to write the equivalent of:
If a condition holds, initialize my variable with some expression; and if the condition ...
0
votes
1
answer
92
views
Optional's orElse() executed even when object is present [duplicate]
I've encountered an unexpected behavior while using the Optional API in Java.
Session session = sessionService
.getSessionById(id)
.orElse(
...
1
vote
0
answers
22
views
Why Optional lambda replace with method reference Causing NullPointerException [duplicate]
There is a A Class
class A {
private String str;
public void setStr(String str) {
this.str = str;
}
}
this method works fine;
public static void main(String[] args) {
...
0
votes
0
answers
19
views
fetching rows from db that are selected by dropbar
guys i wrote a dropdown bar connected to a db, but i couldnt make the content of the table thats fetched from db only specified by the option i chose in drop bar, how can i fix this
here's my code:
$...
-1
votes
1
answer
92
views
How to force non-optional type (for array element) on compile time in Swift? [closed]
struct MyStruct {
@ArrayOfNonOptionalElements var arr: [SomeNonOptionalType]
}
where @ArrayOfNonOptionalElements is a propertyWrapper.
So, how to force non-optionality for the type inside? Is it ...
-1
votes
1
answer
122
views
Generate a type from a Java DTO type with many fields being null and have all getters as Optional<T>
I am currently working with a Java DTO class (written by others) that has almost 300 fields where many of these end up being null. What I see in the codebase are lots of null checks and more recently ...
-4
votes
2
answers
45
views
How to pass a positional argument from one function to *args in another function in python
I have a two scripts and a custom email module with two Functions that works.
But I need a single generic function in the custom email module used by both the scripts .
Script 1
import email_module....
0
votes
3
answers
71
views
Springboot: How to get an entity optional property and check null?
I have a service in Springboot and I want to get an optional entity and to check if is not null to do something after.
User repository class
public interface UserRepository extends JpaRepository<...
0
votes
1
answer
31
views
Typescript Maybe, error about null not being covered
I don't know how to get rid of this error, and I don't know why it talks about null.
Shouldn't the seo?.breadcrumbs || [] already cover the case where breadcrumbs is null?
However in the Intro Propos, ...
2
votes
2
answers
116
views
Optional.ifPresent throws NPE when action is null AND optional not present
Why does this throw a NullPointerException?
The docs for Java 8 state that:
"If a value is present, invoke the specified consumer with the value, otherwise do nothing."
public static ...
2
votes
2
answers
82
views
Monad Map with two values in Java Functional Programming
Forgive me if this is a basic question in FP. Let's consider Optional monad. I know I can use map to transform an Optional to another Optional based on a function (which will be called if optional has ...
2
votes
1
answer
154
views
Chain an "if let" with a boolean expression
I am trying to chain an if let condition with a regular boolean condition.
if let Some(char_first_index) = char_info.first_index_in_string && !char_info.repeats_in_string
However, when I try ...
0
votes
0
answers
30
views
Show CSS in Option of Dropdownlist
how can I make the CSS part be displayed on the options in my dropdown list?
Here is a example what i want: codepen.io/luisaof392/pen/wvOjVGE
there you can actually recognize what I want
I'm new to ...
0
votes
1
answer
79
views
How to get subslice of Options instead of Option of subslice?
From documentation, get method, given a range, returns an Option of subslice which is None if range is out of bounds:
let v = [10, 40, 30];
assert_eq!(Some(&[10, 40][..]), v.get(0..2));
assert_eq!(...
2
votes
2
answers
90
views
Swift, Why are the memory sizes of 'String' and 'String?' the same?
Case Int, Int?
I realized that in the case of optional variables, a bit is needed to check if nil, so it comes out 1 byte larger.
But why is the memory size of String and String? the same?
Not only ...
0
votes
1
answer
56
views
Optional and complete method
How to work correctly with Optional if I need to complete a method (with some actions, log for example) in case of an empty Optional? Otherwise, get the object from Optional and continue working
I ...
1
vote
0
answers
59
views
Pure functional way of creating a std::optional counterpart
I'm working on a pure functional library for c++ and I met a design problem. I was creating the monad Option. This is my implementation
namespace ns {
struct _NoValue_t {};
const _NoValue_t ...
0
votes
1
answer
87
views
Equivalent of "Optional" (maybe type) from Java in Powershell
Citing wikipedia:
In programming languages (especially functional programming languages)
and type theory, an option type or maybe type is a polymorphic type
that represents encapsulation of an ...
0
votes
2
answers
82
views
Is using a wrapper with a 'check' function a good approach to optional types rather than the traditional method? [closed]
languages like C++ and Rust have an Option type, which is basically an enum/bool with a value.
But I feel like this can have a few problems like:
the extra overhead of returning/passing an extra ...
1
vote
4
answers
227
views
What is the best way to check if the value inside an option inside a result satisfies a condition in rust?
I have a function that returns something like Result<Option<Value>, Error>, and I want to check if there is a value and if it matches a condition. There are two straightforward ways:
if ...
2
votes
2
answers
89
views
Getopt::Long, Optional Arguments, and Greediness
I'm trying to write a program that has an option that takes an
optional argument, in such a way that it accepts options in the same
fashion with the exact same behavior as perl -i or git --color. ...
5
votes
1
answer
95
views
In Frege how can I convert an String safely to a Maybe Int?
In Frege I want to convert an String to an Int, but need to handle unparsable strings too.
So I guess I'm looking for something like a readMaybe function. Where do I find this? Or how can I use Java's ...
0
votes
1
answer
122
views
Is there a recommended idiomatic way to adapt a Java Optional to a Kotlin nullable?
I am writing Kotlin code that uses a Java library which uses Optionals, and I would prefer to use Kotlin's nullable idiom in Kotlin-world.
For example, my library may have a function
Optional<Foo&...
5
votes
2
answers
139
views
How to convert Option<Option<String>> to Option<Option<&str>> in Rust?
I am trying to make this conversion (Option<Option<String>> to Option<Option<&str>>) possible, but I still failed after trying many methods including using .map.
I know ...
0
votes
1
answer
227
views
Rust Rayon collect Options into Vector
I am trying to use rayon par_iter with map (and enumerate).
However, the closure returns an Option.
Is there a way to collect the Some value of that option while filtering out Nones?
What I do ...