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,820
questions
4
votes
2
answers
1k
views
Is it better to return an Option<Vec<_>> or just an empty Vec<_>?
Suppose I am writing a function that takes a bunch of strings and filters out the "bad" ones.
The function then returns some strings, but there is a chance that all the strings are filtered ...
1
vote
2
answers
87
views
Create a spark dataset that has additional option fields using an existing dataset
I have a case class as follows:
case class student_address(studentId:String, address:String)
The values from a csv are read into the dataset - studentAddressDS, which is mapped to the above case ...
0
votes
2
answers
701
views
How to assign a value to a None Option when we use as_mut()?
For example, if we have the following struct:
struct A {
value: Option<i32>,
}
fn main() {
let mut a = A { value: Some(1) };
// want to update a's value
match a.value.as_mut() {
...
0
votes
1
answer
99
views
How to flatten if-else-if-statements with caching in Scala 3?
I have the following very ugly nesting of if-statements that involve slow operations (tryFindResult and tryFindOtherResult) that I need to execute as little as possible. Just to give some context, it'...
3
votes
1
answer
95
views
Scala `for - yield` gives Option of Tuple, but need Tuple of Option's
object main {
def main(args: Array[String]) = {
val x: Option[Int] = Some(2)
val y: Option[Int] = Some(3)
val z: Option[Int] = Some(5)
val result1 = for {
x <- x
y &...
1
vote
1
answer
58
views
Scala for-comprehension and Option
object main {
def main(args: Array[String]) = {
val x = Option(2)
val y = None // works with: Option.empty[Int]
val z = Option(5)
val result1 = for {
x <- x
y <- y
...
0
votes
0
answers
108
views
Does using java.util.Optional affect memory/speed and how does JIT Compiler handle it?
I've been using java.util.Optional extensively in my code for better null handling. I even went as far as creating new "safe" datastructures with stuff like:
public Optional<Integer> ...
1
vote
0
answers
60
views
Is there a built-in function to query a type hint for optionality/"None-ability" in Python 3.10 or later?
Is there a function in the standard library to query whether the type hint for a field admits the None value?
For example, it would return True for foo, bar, baz, and False for x, in class A below:
...
1
vote
3
answers
263
views
guard/if let in Java - declare a property or an object if it can be unwrapped
In swift there is the guard let / if let pattern allowing us to declare an object or a property only if it can be unwrapped.
it works a follow:
func getMeaningOfLife() -> Int? {
42
}
func ...
5
votes
4
answers
249
views
In Functional Programming, why the Maybe functor is not defined as data Maybe a = Nothing | a?
I was wondering why in Haskell (and FP in general) functors such as the Maybe functor are defined as
data Maybe a = Nothing | Just a
Instead of simply
data Maybe a = Nothing | a
The second option ...
-7
votes
3
answers
739
views
Is there a concise way to check if an Option is None or if its Some contents are empty? [closed]
Is there something more concise that the below code?
let player_list: Option<Vec<Player>>;
// more code here
if player_list.is_none() || player_list.as_ref().unwrap().is_empty() {
// ...
0
votes
0
answers
39
views
TypeScript + Property 'nothing' does not exist on type '<T>(x: T)
I'm trying to hone my Typescript monad skills, so I have created a version of the Maybe monad that technically seems to be working at this point, but Typescript is complaining that Property 'nothing' ...
0
votes
2
answers
64
views
Null safe way to deoption and avoid type mismatch
While giving some readability advice here and checking that my rewrite hadn't broken anything, I discovered it was already broken.
case class Foo (
bars: Seq[Bar]
)
case class Bar(s: String)
def ...
2
votes
1
answer
270
views
What Exactly is the Proposed C23 `_Either` Type?
C23 proposal n3003 and n2366 mention a proposed _Either type in passing on the first page and seventh, respectively, and I have not been able to find any other references to it thus far. As far as I ...
2
votes
1
answer
207
views
Why does calling a virtual class's virtual method inside std::optional generate a call to the base method, not an overridden?
Suppose we have some virtual class Callable, (not abstract, because we cannot use abstract classes as type of std::optional<T>), and some function (call_option) that takes an std::optional of ...