question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic defaults?

See original GitHub issue

I have a generic method that is used in the Program.Main for some microservices. I want want to reuse options, but have different defaults.

Is there a way to specify the defaults that aren’t compiled (property attributes)?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rmunncommented, Aug 19, 2020

This could possibly be handled by having a DefaultFactory property in the Option and Value attributes. Functions can’t be attribute parameters, but you could make DefaultFactory be a string that could be the name of a method of your options class, e.g.:

[Option("foo", DefaultFactory=nameof(CalculateValue))]
string Foo { get; set; }

[Option("num")]
int Number { get; set; }

public string CalculateValue() {
    return (this.Number > 0) ? "positive" : (this.Number == 0) ? "zero" : "negative";
}

Then the Parser would first apply all default values specified by Default="some-constant-value", then for any DefaultFunc definitions it would use reflection to look up that name as a method on the options instance and call the method.

Wouldn’t be too hard to implement this, now that I think about it. @pauldotknopf - Would this idea fit your needs?

0reactions
rmunncommented, Aug 20, 2020

Nope, can’t be done. By the time reflection is happening (at run-time), the nameof(Foo) expression found in the source code has been turned into the literal "Foo" at compile-time, and there is no way for any runtime code to tell whether it started as "Foo" or nameof(Foo). (You can try it yourself if you think I’m wrong). It will have to have a different name than Default. Possible names are DefaultFactory as I suggested, or DefaultProperty as suggested in https://github.com/commandlineparser/commandline/issues/191 for the HelpTextProperty. After reading that discussion, I’m leaning more towards DefaultProperty, because that gives more of a hint to the library user about what “shape” they should give it. (A “Factory” name should indicate that it should be a function that takes no parameters, but if you have a function then someone sometime is going to want to put parameters on it. Calling it FooProperty indicates that it should be a simple property with a get method, which can not (by definition) take parameters.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating parameter defaults in Amazon QuickSight
Using dynamic defaults involves some preparation to map people to their assigned defaults. First, you need to create a database query or a...
Read more >
How to make Dynamic parameter - Question & Answer
One simple way to create the Dynamic Defaults dataset with your default dates (without requiring any storage) is to create an Athena dataset ......
Read more >
NetSuite Applications Suite - Dynamic Defaults and ...
Dynamic defaults can be used in the hyperlink fields to include information from the record or the current session in the URL for...
Read more >
Goldman Sachs Dynamic Default
With Goldman Sachs Dynamic Default, a professionally managed asset allocation strategy, we aim to meet participants where they are in their retirement journey....
Read more >
[FF2] [Abilities] Dynamic Defaults v1.3.2 (many new adds!)
This pack contains mobility abilities, abilities that are mainly for developers and don't truly shine without another developer's ability (i.e. ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found