Bug 9183 – Add a Nullable.get(x) overload

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-12-18T20:16:08Z
Last change time
2017-10-16T09:57:27Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-12-18T20:16:08Z
I suggest to add an optional argument to Nullable.get() (similar to associative array get()), that returns the given datum if the nullable is empty. This overload with one argument is nothrow: import std.typecons; nothrow void main() { Nullable!int n; int x = n.get(10); // x becomes 10. } See also Issue 9086 This idea is meant to allow some usage of Nullable in nothrow function, and to make Nullable more handy to use in some cases. This idea is similar to the getOrElse method of the Option Scala object: http://www.scala-lang.org/api/current/scala/Option.html See also in that module how many methods Scala Option has to make its usage more handy and increase safety in absence of null.
Comment #1 by bus_dbugzilla — 2013-02-16T15:23:31Z
This would indeed be a great thing to have.
Comment #2 by github-bugzilla — 2017-08-22T16:03:15Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/1c071c2176b3ef4552f70e4fefc78f4ebe9d748c Fix Issue 9183 - Add a Nullable.get(x) overload https://github.com/dlang/phobos/commit/752fc0323a92d37056bb373cf8b91885a6406320 Merge pull request #5695 from RazvanN7/Issue_9183 Fix Issue 9183 - Add a Nullable.get(x) overload merged-on-behalf-of: Jack Stouffer <[email protected]>
Comment #3 by github-bugzilla — 2017-10-16T09:57:27Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/1c071c2176b3ef4552f70e4fefc78f4ebe9d748c Fix Issue 9183 - Add a Nullable.get(x) overload https://github.com/dlang/phobos/commit/752fc0323a92d37056bb373cf8b91885a6406320 Merge pull request #5695 from RazvanN7/Issue_9183