While my question below refers to a specific method - tryParse() - I mean it just as an example, and I look at below as a general problem on class extensions.
Sometimes it would make sense to call tryParse() on an instance object instead of a type. Let me show what I mean.
This is a valid code in dart:
static double dummy(String s) {
double? d = double.tryParse(s);
if (d == null) {
d = 0;
// do some other meaningful stuff here
};
return d;
}
Instead of above, it would be great to call mySmartParser() on the variable d:
static double dummy(String s) {
double d;
if (!d.mySmartParser(s, 0)) {
// do some other meaningful stuff here
};
return d;
}
If I write the extension - mySmartParser() - on type double, then this is still valid only on the type, but can't invoke it on a variable. Why ? Can this be solved without boiling the ocean ?
I know I could do like below, but then how to handle the "do some other meaningfule stuff" branch ?
double d = double.tryParse(s) ?? 0;
While my question below refers to a specific method - tryParse() - I mean it just as an example, and I look at below as a general problem on class extensions.
Sometimes it would make sense to call tryParse() on an instance object instead of a type. Let me show what I mean.
This is a valid code in dart:
static double dummy(String s) {
double? d = double.tryParse(s);
if (d == null) {
d = 0;
// do some other meaningful stuff here
};
return d;
}
Instead of above, it would be great to call mySmartParser() on the variable d:
static double dummy(String s) {
double d;
if (!d.mySmartParser(s, 0)) {
// do some other meaningful stuff here
};
return d;
}
If I write the extension - mySmartParser() - on type double, then this is still valid only on the type, but can't invoke it on a variable. Why ? Can this be solved without boiling the ocean ?
I know I could do like below, but then how to handle the "do some other meaningfule stuff" branch ?
double d = double.tryParse(s) ?? 0;
Share
Improve this question
asked Feb 15 at 19:23
László FrankLászló Frank
431 silver badge2 bronze badges
2
|
1 Answer
Reset to default 2What you are trying to do cannot work in Dart.
If I understand the idea correctly, the intend is that d.mySmarParser(s, 0)
should behave as d = double.tryParse(s) ?? 0
.
That is, you are not trying to invoke a member on a double value, but on a double variable, giving it write access to the variable. Dart does not have first class variable references, so you cannot abstract over a variable.
You're not allowed to do that invocation because d
does not refer to a value at that point. It's an uninitialized variable which means it has no value. Even if you could invoke an extension member on the d
value, it won't be able to change the value of the underlying variable.
A language like Java allows you to invoke static members on instances. It might allow something like d.tryParse(s)
, but it's just shorthand for double.tryParse(s)
where double
is the static type of d
. It also won't allow what you want to do here.
You need to be able to represent a reference to a variable of type double
as a value in order to pass it to another function.
In C#, you could have
public static void TryParseDouble(
String input, double defaultValue, out double result) {
try {
result = Double.parse(input);
} catch {
result = defaultValue;
}
}
The out
parameter is a reference parameter. C# has both ref
and out
, where ref
can be assigned to, and out
must be assign a value by the function.
(They actually have that function).
Again, Dart does not have reference parameters at all, you cannot abstract over variables, only over values. What you're trying to do here cannot be made to work without first class references.
this
. A static method declared within an extension does not refer to the extended type. In your example, it is not clear whatstatic
refers to. – Dan R Commented Feb 17 at 13:18