Timeline for What is the idiomatic Go equivalent of C's ternary operator?
Current License: CC BY-SA 4.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 18, 2024 at 7:08 | comment | added | StainlessSteelRat | This is absolutely brilliant but it fails for something like this: When[int*](somePtrRecord != nil).Then(&somePtrRecord.intVal).Else(nil) because the Then value is evaluated | |
| Aug 27, 2024 at 15:37 | comment | added | Rob Spoor |
What you can possibly do is reorder things. It will then look like a Python ternary expression. For instance: Return(value).When(value > 0).Else(1), Answer(func()int { return value * 4 }).When(value > 0).Else(1). I've taken the "answer" part from Mockito, as it also supports this ordering, starting with doReturn, doAnswer, doThrow, etc.
|
|
| Dec 28, 2023 at 13:09 | comment | added | Tommaso Resti |
Imagine, for example, that you want to call an endpoint if a certain condition is true. Calling the endpoint inside the Then would execute the call in any case, whether the condition is true or false. Calling it in the ThenDo would only execute the call if the condition is true
|
|
| Dec 28, 2023 at 13:05 | comment | added | Tommaso Resti |
it allows lazy evaluation of the code in case for example you want to execute an expensive function on the thenDo. With only the Then and Else you are required to precalculate the results of the two branches and sometimes you just want to avoid that
|
|
| Dec 6, 2023 at 20:18 | comment | added | MrLowbob | readability wise this is nice. feels a lot better than the 3 argument version. perhaps im missing something, but as the thenDo method still returns a value by directly evaluating the provided function, why would i ever put something in an anonymous function there, and just return "value * 4" directly? so I could dynamically throw in other functions in there or ..? | |
| Dec 6, 2022 at 14:31 | history | edited | Tommaso Resti | CC BY-SA 4.0 |
added 251 characters in body
|
| Dec 6, 2022 at 10:30 | history | edited | Tommaso Resti | CC BY-SA 4.0 |
added 181 characters in body
|
| Dec 6, 2022 at 10:09 | history | edited | Tommaso Resti | CC BY-SA 4.0 |
added 72 characters in body
|
| Dec 6, 2022 at 10:04 | history | answered | Tommaso Resti | CC BY-SA 4.0 |