On this page10 sections
Lecture 050: Callable Classes
| Day | Extra Topics |
| Lecture | 050 of 057 |
| Estimated video length | 8 min |
| Target | Dart 3.12 |
| Level | Beginner |
Give a class a method named
call, and you can use its objects like functions:greeter('Ali')instead ofgreeter.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
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:
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
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
Rs 1000.0 → Eid sale: Rs 800.0, student: Rs 900.0
Rs 5000.0 → Eid sale: Rs 4500.0, student: Rs 4500.0Each 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
class Greeter {
String greet(String name) => 'Hi, $name';
}
var g = Greeter();
print(g('Ali'));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
- What must the method be called to make a class callable?
- With
var t = Multiplier(4);, what ist(5)? - Are
t(5)andt.call(5)the same? - Write a callable class
Adderthat adds a fixed number to whatever it's given.
Practice Answers
call20- Yes. The first is shorthand for the second.
class Adder {
final int amount;
Adder(this.amount);
int call(int value) => value + amount;
}Key Takeaways
- A method named
calllets 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.