Daniel Robinson
Daniel Robinson

Reputation: 14878

can you add properties on to functions in Dart?

Can you add properties on to functions in dart? I tried this:

void main(){

    fn(){
        //DoSomething
    };

    fn.id = 1; //Exception NoSuchMethod

}

Only to get a NoSuchMethod exception thrown. Is there any way to add properties to functions or any type of object for that matter at runtime like in javascript?

(Also why is it saying no such method exception when I'm assigning to a property?)

Upvotes: 2

Views: 1518

Answers (3)

TastyCatFood
TastyCatFood

Reputation: 1762

Simply put no.
Expando can mimic that though.

var e = new Expando();
e[f] = 'hi';
print(e[f]);//prints hi

Advertising my package might be inappropriate, but since its related:

import 'package:mistletoe/mistletoe.dart';
Dynamism d = new Dynamism(expert:true);
void main(){
    var o = new Object();
    d.on(o).greetings = ()=>print('hello world');
    d.on(o).greetings();//prints hello world
    o = null;
    //With o garbage collected, d is empty now. 
}

git:https://github.com/TastyCatFood/mistletoe

There are also packages like this: https://pub.dartlang.org/packages/dynamic_object which seems to override noSuchMethod to create a custom class that allows a pseudo dynamic addition of properties. If you extend the class Function and override noSuchMethod, I'm sure you can create a function object that allows you to add properties.

Generally speaking, you should avoid making your code too dynamic though. Dart analyzer and your IDE does not support these hacky things. Although, there are times you need it.

Upvotes: 0

Florian Loitsch
Florian Loitsch

Reputation: 8128

You can not add or remove members of an object. Conceptually an object is a small structure with a pointer to its class description and some memory to store its state. The class description defines the layout of the structure at allocation point. If the class description doesn't contain a field id (like in your example) there is simply no way to add it. You can neither change the class description nor change the actual instance (so it has more memory for the extra field). This might be annoying (especially for JavaScripters that are used to dynamically modify their objects), but it gives certain guarantees to programmers and makes it easier to optimize. Objects use less memory, and the optimizing compiler can make assumptions that would otherwise not hold.

There is, however, a way to add properties from the side with expandos

Upvotes: 1

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76233

You can not add a property on a Function. However you can define an Object that can be call like a function (see Emulating Functions in Dart). Here's an example :

class WannabeFunction {
  var id;
  call(int a, int b) => a + b;
}

main() {
  var wf = new WannabeFunction();
  wf.id = 1;
  wf(3, 4); // 7
}

For the NoSuchMethodError : when you use fn.id = 1 you actually call an implicite setter id= on fn. That why you get this error.

Upvotes: 6

Related Questions