Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do we need Callable classes in dart

Tags:

flutter

dart

What is the use of callable classes in dart lang? Following is the example code available on official dart site.

class WannabeFunction {
  call(String a, String b, String c) => '$a $b $c!';
}

main() {
  var wf = new WannabeFunction();
  var out = wf("Hi","there,","gang");
  print('$out');
}

How useful is it add a call function and call it using a class instead of creating a function itself in class

like image 379
vashishth Avatar asked Apr 21 '19 04:04

vashishth


People also ask

How do you call a class function in darts?

What is Dart call()? In Dart, all functions are objects with type Function since Dart is a pure object-oriented language. All functions inherently have the call method. The call method is used to call a function like someFunction() .

Is a class method callable?

In Python, classes, methods, and instances are callable because calling a class returns a new instance. Instances are callable if their class includes __call__() method.

What are classes in Dart?

Dart classes are the blueprint of the object, or it can be called object constructors. A class can contain fields, functions, constructors, etc. It is a wrapper that binds/encapsulates the data and functions together; that can be accessed by creating an object.

What is named constructor in Dart?

Dart defines a constructor with the same name as that of the class. A constructor is a function and hence can be parameterized. However, unlike a function, constructors cannot have a return type. If you don't declare a constructor, a default no-argument constructor is provided for you.


1 Answers

This can be useful to make "named functions":

class _Foo {
  const _Foo();

  void call(int bar) {}

  void named() {}
}

const foo = _Foo();

Which allows both:

foo(42);

and

foo.named();
like image 166
Rémi Rousselet Avatar answered Oct 13 '22 14:10

Rémi Rousselet