Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default values. #14

Closed
Wolftein opened this issue Oct 31, 2013 · 1 comment
Closed

Add default values. #14

Wolftein opened this issue Oct 31, 2013 · 1 comment

Comments

@Wolftein
Copy link

Add a way to get a default value if the node does not exist.

Current:

name = node.containsKey("Name") ? node["Name"].as!string : "Default Value";

New

name = node["Name", "Default Value"].as!string;
@kiith-sa
Copy link
Collaborator

I'm not going to implement this (although should be easy to create a UFCS external method if you really want it).

Reasons:

  • I don't like the idea of constructing and then unpacking a Node, because of performance but mainly because it will complicate eventual pure/@safe annotations I'm adding as related issues are fixed in Phobos.
  • I want the API to be more explicit than implicit (make it obvious what you're doing by using containsKey() vs maybe reading the value or maybe not reading it.
  • There's a simple (although more verbose) pattern to achieve the same thing. First declare all variables you're about to read initialized with their defaults, then read all of them in a try/catch block, and silently ignore a throw.
  • And I like the idea that a missing value throws by default (with default values, I would not get that throw; I'd have to explicitly check for the error instead (yeah, this is an opposite side of the implicit/explicit thing))

Yes, I realize most of these are subjective and can be argued for both sides.

Sorry for the (massive) delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants