On this page10 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. The call Method
  4. 2. Objects That Remember Settings
  5. 3. When to Use One
  6. Code Examples
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 050: Callable Classes

DayExtra Topics
Lecture050 of 057
Estimated video length8 min
TargetDart 3.12
LevelBeginner

Give a class a method named call, and you can use its objects like functions: greeter('Ali') instead of greeter.call('Ali'). It's a small feature, but it lets an object remember settings and still behave like a function.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lectures 030–031 (classes and constructors) and 048 (anonymous functions).


Learning Objectives

  • Make a class callable with a call() method
  • Call an object as if it were a function
  • Pass a callable object where a function is expected
  • Know when a callable class beats a plain function

1. The call Method

dart
class Greeter {
  final String greeting;
  Greeter(this.greeting);

  String call(String name) => '$greeting, $name!';
}

void main() {
  var hello = Greeter('Hello');
  print(hello('Ayesha'));        // Hello, Ayesha!
  print(hello.call('Bilal'));    // Hello, Bilal!  (the long way, same thing)
}

hello('Ayesha') looks like a function call, but hello is an object. Dart sees the brackets and runs its call method.

The method must be named exactly call. It can take any parameters and return any type.

2. Objects That Remember Settings

A plain function forgets everything between calls. A callable object keeps its fields:

dart
class Multiplier {
  final int factor;
  Multiplier(this.factor);

  int call(int value) => value * factor;
}

var triple = Multiplier(3);
print(triple(7));                        // 21
print([1, 2, 3].map(triple).toList());   // [3, 6, 9]

triple is passed straight into map, just like a function, because it can be called like one.

3. When to Use One

  • You need a function plus some settings (a factor, a discount rate, a greeting).
  • You want the setup in one place (the constructor) and the action to be a simple call.

For a one-off job with no settings, a normal function is simpler.


Code Examples

Example 1: Discount Rules

dart
class DiscountCalculator {
  final double percent;
  final double maxDiscount;
  DiscountCalculator(this.percent, {this.maxDiscount = double.infinity});

  double call(double price) {
    var discount = price * percent / 100;
    if (discount > maxDiscount) discount = maxDiscount;
    return price - discount;
  }
}

void main() {
  var eidSale = DiscountCalculator(20, maxDiscount: 500);
  var studentDeal = DiscountCalculator(10);

  for (var price in [1000.0, 5000.0]) {
    print('Rs $price → Eid sale: Rs ${eidSale(price)}, student: Rs ${studentDeal(price)}');
  }
}

Output

code
Rs 1000.0 → Eid sale: Rs 800.0, student: Rs 900.0
Rs 5000.0 → Eid sale: Rs 4500.0, student: Rs 4500.0

Each calculator remembers its own rules. On a Rs 5000 item, the Eid sale would take 20% (Rs 1000), but its Rs 500 limit kicks in.


Common Beginner Mistakes

1. Naming the method anything other than call

dart
class Greeter {
  String greet(String name) => 'Hi, $name';
}

var g = Greeter();
print(g('Ali'));
code
Error: The method 'call' isn't defined for the type 'Greeter'.

Rename greet to call, or write g.greet('Ali').

2. Forgetting to create the object first: Multiplier(7) creates a multiplier with factor 7; it doesn't multiply anything. You need Multiplier(3)(7), or better, store it first: var triple = Multiplier(3); triple(7);

3. Using a callable class where a function is enough: if there are no fields, write a normal function.


Practice Questions

  1. What must the method be called to make a class callable?
  2. With var t = Multiplier(4);, what is t(5)?
  3. Are t(5) and t.call(5) the same?
  4. Write a callable class Adder that adds a fixed number to whatever it's given.

Practice Answers

  1. call
  2. 20
  3. Yes. The first is shorthand for the second.
dart
class Adder {
  final int amount;
  Adder(this.amount);

  int call(int value) => value + amount;
}

Key Takeaways

  • A method named call lets you use an object like a function: obj(args).
  • Callable objects can be passed anywhere a function is expected.
  • They're useful when a function needs to remember settings.
  • With no settings, a plain function is simpler.