Skip to content

Cumbersome to conditionally pass Value.absent() #1209

Answered by simolus3
fgblomqvist asked this question in Q&A
Discussion options

You must be logged in to vote

Thank you for starting this discussion. I've added Value.ofNullable which is similar, except that it checks that T is non-nullable as well:

Value<int>.ofNullable(null) -> absent
Value<int>.offNullable(12) -> present
Value<int?>.ofNullable(null) -> invalid (assertion error)! the null value could be both absent and present
Value<int?>.ofNullable(12) -> present

Until that is released, you can use your own function for that:

Value<T> valueOf<T>(T? value) {
  assert(value != null || null is! T);
  return value != null ? Value(value) : const Value<Never>.absent()`
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@fgblomqvist
Comment options

Answer selected by fgblomqvist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants