最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Invoke extension method on an instance not a type in Dart - Stack Overflow

programmeradmin3浏览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;

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 Dart extension methods typically work on objects of the type on which they are declared. See for some examples of this: medium/dartlang/extension-methods-2d466cd8b308 --- If that is now how it works for you, that is likely due to how you implement the extension method. "I write the extension - mySmartParser() - on type double" Please edit your question to show how you've done this. – Frank van Puffelen Commented Feb 15 at 21:28
  • It is not clear what you mean by: "it would make sense to call ... on an instance object instead of a type". Extension methods are called on an instance and the instance is available as this. A static method declared within an extension does not refer to the extended type. In your example, it is not clear what static refers to. – Dan R Commented Feb 17 at 13:18
Add a comment  | 

1 Answer 1

Reset to default 2

What 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.

发布评论

评论列表(0)

  1. 暂无评论