I am wanting to do something like this:
void function() test = void function() { ... };
Is this possible? Also can I make function arrays like this:
void function()[] test = [
void function() { ... },
void function() { ... }
];
I know I could just use function pointers, but for my purpose the functions don't actually need a name, and will only be accessed from the array, so it seems quite redundant to give each one of them a declaration.
Yup, and you almost guessed the syntax. In the function literal, "function" goes before the return type:
void function() test = function void () {...};
Much of that is optional. This does the same (as long as the compiler can infer everything):
auto test = {...};
Further reading: http://dlang.org/expression.html#FunctionLiteral, http://dlang.org/function.html#closures
Also, you can just nest functions in D:
void main()
{
void test() {...}
test();
}
Related
I am new to Dart and Flutter, I wanted to know what i the actual difference and when to use which one.
A function is a top-level function which is declared outside of a class or an inline function that is created inside another function or inside method.
A method is tied to an instance of a class and has an implicit reference to this.
main.dart
// function
void foo() => print('foo');
// function
String bar() {
return 'bar';
}
void fooBar() {
int add(int a, int b) => a + b; // inline function
int value = 0;
for(var i = 0; i < 9; i++) {
value = add(value, i); // call of inline function
print(value);
}
}
class SomeClass {
static void foo() => print('foo'); // function in class context sometimes called static method but actually not a method
SomeClass(this.firstName);
String firstName;
// a real method with implicit access to `this`
void bar() {
print('${this.firstName} bar');
print('$firstName bar'); // this can and should be omitted in Dart
void doSomething() => print('doSomething'); // inline function declared in a method
doSomething(); // call of inline function
}
}
Like inline functions you can also create unnamed inline functions, also called closures. They are often used as callbacks like
button.onClick.listen( /* function start */ (event) {
print(event.name);
handleClick();
} /* function end */);
Shouldn't this (or something like it) work? and/or How can I accomplish this some other way?
class A {
void init() {initializeSomething()}
}
main() {
var f = A.init;
A a = new A();
a.f();
}
I want, in general, to store a reference to an instance method
somewhere, then call it somewhere else (something I can do in most other
languages, including JavaScript). I thought functions were first class
in Dart... and aren't methods functions? Are methods not first class?
It works as long as the method is static (which is of no use in my case) but not for instance methods...
Functions and methdods are different. You can only call methods on an instance of a class that has this method.
In your example (var f = A.init) you reference an instance method like a static (class) method.
What works is:
make init static
class A {
static void init() => initializeSomething();
// or
// static void init() {
// initializeSomething();
// }
}
main() {
var f = A.init;
A a = new A();
a.f();
}
or use a reference to init() of an actual instance of A:
class A {
void init() => initializeSomething();
}
main() {
A a = new A();
var f = a.init;
a.f();
}
Refer to this small top-of-my-head coed snippet:
typedef void __fastcall (__closure *GetHTTPCallBack)(String filename);
class Foo {
private:
public:
GetHTTPCallBack Callback;
__fastcall Foo (void) { Callback = NULL; }
__fastcall Foo (GetHTTPCallBack callback) { Callback = callback; }
};
class Bar {
private:
Foo *foo;
public:
void __fastcall CallbackFunction(String fileName) { // Do something }
void __fastcall SetByConstructor(void) { foo = new Foo(CallbackFunction); }
void __fastcall SetByAssignment (void) { foo = new Foo(); foo->Callback = CallbackFunction; }
};
Bar bar;
Now to the problem.
If Call bar.SetByAssignment() it works fine, the function pointer is set and called correctly from Foo. But in this case it is exposed as a public variable. I would like it to be private.
I would like to simplify the class, and hide it to be private, by passing the function pointer in the constructor, bar.SetByConstructor() but here I get compiler error:
[bcc32 Error] : Member function must be called or its address taken
I thought the __closure would make this possible. Is it possible at all or I'm I just doing it wrong?
Do what the compiler tells you. Pass the memory address of the method. How do you get the memory address of anything? Use the & address operator:
foo = new Foo(&CallbackFunction);
It is good habit to do that when using an assignment as well (though in that case it is optional):
foo->Callback = &CallbackFunction;
what does call(thisArg:*, ... args) first parameter mean?
Assuming f() is defined in a unnamed package as global function, following is the code snippet:
package {
public function f(message:String):void {
trace(message);
trace(this.watchedValue);
}
}
test code as following:
public function test():void {
var obj:Object = {watchedValue:100};
f("invoking f");
f.call(obj, "invoking f by call()");//actual result is undefined, but shouldn't be 100?
}
This param only used in closures and anonymous functions, like
var testFunc:Function = function():void{trace(this.watchedValue)}
EDIT:
in you case it will be
package {
public var f:Function = function(message:String):void {
trace(message);
trace(this.watchedValue);
}
}
EDIT2
first parameter of call will be this in called function. This is the way to call fauction like a method of object.
But when function is method or top level function first parameter of call() will be ignored. To use first param your function must be variable with anonymous function.
As far as I know Function.call() is the same as function(), except the fact you change the scope of this. Normally this referrers to the current class, but it could be another class. \
Your test function looks wrong, it should be obj instead of o
public function test():void {
var obj:Object = {watchedValue:100};
f("invoking f");
f.call(obj, "invoking f by call()");
}
This is my code:
public function setPaneContent(names : Array, parent : AbstractPane) : void {
//....
okButton.addEventListener(MouseEvent.CLICK, function okMouseClickHandler(e : Event) : void {
parent.addNewPane(valuesPane, parent);
PopUpManager.removePopUp(/*need to put "this"*/);
});
//.....
}
When i call PopUpManager.removePopUp(/*need to put "this"*/);, i need to make a reference at the object that contains this method(this).
So my question is: "Is it possible to make a reference at 'this' keyword within an anonymous method?"
store this to some variable: _this = this in the constructor, use _this. (it works in javascript)
You don't have to, you can call another function
public function setPaneContent(names : Array, parent : AbstractPane) : void
{
okButton.addEventListener(MouseEvent.CLICK,
function okMouseClickHandler(e : Event) :void
{
parent.addNewPane(valuesPane, parent);
// call the remove function where you can reference "this"
remove();
});
//.....
}
private function remove():void
{
PopUpManager.removePopUp(this);
}