Menu
Coddy logo textTech

Callable Classes

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 84 of 110.

In Dart, you can make instances of a class callable like functions by implementing the call() method. This allows you to use an object with function call syntax - using parentheses directly on the instance.

class Multiplier {
  final int factor;
  
  Multiplier(this.factor);
  
  int call(int value) {
    return value * factor;
  }
}

void main() {
  var double = Multiplier(2);
  var triple = Multiplier(3);
  
  print(double(5));   // 10 - calling the object like a function
  print(triple(5));   // 15
}

When you write double(5), Dart automatically invokes the call() method with 5 as the argument. The object behaves like a function while still maintaining its state (the factor field).

The call() method can have any signature - multiple parameters, optional parameters, or even return different types:

class Greeter {
  final String greeting;
  
  Greeter(this.greeting);
  
  String call(String name, {String punctuation = '!'}) {
    return '$greeting, $name$punctuation';
  }
}

void main() {
  var hello = Greeter('Hello');
  print(hello('Alice'));              // Hello, Alice!
  print(hello('Bob', punctuation: '.')); // Hello, Bob.
}

Callable classes are useful when you need function-like behavior combined with state or configuration. They're commonly used for creating reusable operations, validators, or transformations that need to remember settings between calls.

challenge icon

Challenge

Easy

Let's build a text formatter system using callable classes! You'll create objects that can be called like functions to transform text in different ways, while maintaining their own configuration settings.

You'll organize your code into two files:

  • formatters.dart: Create two callable classes that transform text:
    • A Wrapper class that wraps text with a prefix and suffix. It should store a String prefix and String suffix, and when called with a String text parameter, return the text wrapped between the prefix and suffix.
    • A Repeater class that repeats text a certain number of times. It should store an int times value, and when called with a String text parameter, return the text repeated that many times with a space between each repetition.
  • main.dart: Import your formatters and demonstrate how callable classes work:
    • Create a Wrapper with prefix "[" and suffix "]"
    • Create another Wrapper with prefix "***" and suffix "***"
    • Create a Repeater that repeats 3 times
    • Call the first wrapper with "Hello" and print the result
    • Call the second wrapper with "Important" and print the result
    • Call the repeater with "Go" and print the result
    • Finally, combine them: call the first wrapper with the result of calling the repeater with "Dart", and print the result

Notice how each formatter object remembers its configuration and can be called directly with parentheses, just like a function!

Expected output:

[Hello]
***Important***
Go Go Go
[Dart Dart Dart]

Cheat sheet

Classes can be made callable like functions by implementing the call() method:

class Multiplier {
  final int factor;
  
  Multiplier(this.factor);
  
  int call(int value) {
    return value * factor;
  }
}

void main() {
  var double = Multiplier(2);
  print(double(5));   // 10 - calling the object like a function
}

The call() method can have any signature with multiple parameters, optional parameters, or different return types:

class Greeter {
  final String greeting;
  
  Greeter(this.greeting);
  
  String call(String name, {String punctuation = '!'}) {
    return '$greeting, $name$punctuation';
  }
}

void main() {
  var hello = Greeter('Hello');
  print(hello('Alice'));              // Hello, Alice!
  print(hello('Bob', punctuation: '.')); // Hello, Bob.
}

Callable classes are useful for creating function-like behavior combined with state or configuration.

Try it yourself

import 'formatters.dart';

void main() {
  // TODO: Create a Wrapper with prefix "[" and suffix "]"
  
  // TODO: Create another Wrapper with prefix "***" and suffix "***"
  
  // TODO: Create a Repeater that repeats 3 times
  
  // TODO: Call the first wrapper with "Hello" and print the result
  
  // TODO: Call the second wrapper with "Important" and print the result
  
  // TODO: Call the repeater with "Go" and print the result
  
  // TODO: Combine them: call the first wrapper with the result of
  // calling the repeater with "Dart", and print the result
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming