-
Notifications
You must be signed in to change notification settings - Fork 205
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
is there a better way to change any fields' value with only one Void? #592
Comments
The ability to access fields of an object as if it had been a map from something (say, Maybe you actually just want to use a In general, Dart has been moving in the direction of static typing for quite a while now (and we plan to move further in that direction with features like 'non-null by default' and variance). So there are a lot of good reasons for thinking really hard about your software design, and trying to do what you need to do without this kind of inherently-dynamic features. |
Indeed. What you are doing is a kind of reflection where you use a run-time string value to represent a source name. The only way to refer to a variable by source name in Dart is to use the name in the source code. At run-time, the names are no longer available (you may see them in error messages or similar, but only as values, not as a way to refer to the thing). What you can do here is either the switch case you have, or some other structure like: var _settes = {
"x" : (String value) { x = value; },
"y" : (String value) { y = value; },
"z" : (String value) { z = value; },
};
void changeFieldValue(String field, String value) {
_setters[field](value);
} In any case, the |
I disagree. Before I started using Dart (with Flutter) my primary language was a completely statically-typed one (and one that doesn't even have a built-in Here's a 15 line void changeFieldValue(Object, Field)(ref Object obj, string fieldName, Field newFieldValue)
{
final switch (fieldName)
{
static foreach (idx, field; Object.tupleof)
{
case field.stringof:
{
static if (is(typeof(field) == Field))
obj.tupleof[idx] = newFieldValue;
return;
}
}
}
} ( The only "special" runtime feature (compared to C) used here is the ability to switch on strings which Dart already has. All the rest happens at compile-time:
What we're doing in the code snippet above is generating the list of The end result, given a class type like e.g. void changeFieldValue(ref C obj, string fieldName, int newFieldValue)
{
final switch (fieldName)
{
case "a":
obj.a = newFieldValue;
return;
case "b":
obj.b = newFieldValue;
return;
}
} I really wish Dart could support these features as they greatly reduce the amount of boilerplate code and dramatically increase the design space for library authors. Here's a full example: https://run.dlang.io/gist/ZombineDev/e5f2ab17ff0a05ea342c7c7e37e77ce3?compiler=dmd |
@ZombineDev wrote:
Interesting stuff! Static meta-programming (with access to information about the resolution of name applications to name declarations, as well as static analysis results like typing information) can allow for very elegant solutions, and the integration with the language and the compilation process can allow for a smooth development workflow. We have discussed adding support for various kinds of static meta-programming to Dart, and it would be so cool, but it's a large feature and we don't have concrete proposals. For now, we'd have to use more traditional code generation techniques (which may use the analyzer to get access to the results of static analysis) in order to get similar outcomes. |
Hi,
What is the best practice to change field value dynamically in dart?
The below code looks fine for a class has 2 - 4 fields, but it will be too much repetition when the class is growing.
do dart have a feature like below code ?
I am sorry if this is not a right place to ask.
thanks
void changeFieldValue(String field, String val) => this[field] = val;
The text was updated successfully, but these errors were encountered: