Method Not Found Fallback

Published: (February 17, 2026 at 10:22 AM EST)
4 min read
Source: Dev.to

Source: Dev.to

Overview

This is part eleven in the Cases of UPPER series, describing the Raku syntax elements that are completely in UPPERCASE.
In this part we discuss the interface method that can be provided in a class to handle calls to methods that do not actually exist in the class (or its parents).

Method Lookup in Raku

Method dispatch in the Raku Programming Language is quite complicated, especially with multi‑dispatch. The first step is to see if there is any (multi) method with the given name. Internally this is handled by the find_method method on the meta‑object of the class (typically called with the .^method syntax).

# an empty class
class A { }

dd A.^find_method("foobar");  # Mu
dd A.^find_method("gist");    # proto method gist (Mu $:: |) {*}

An empty class A (which inherits from Any) does not provide a method foobar (indicated by the Mu type object), but it does provide a gist method (inherited from Any).

If the dispatch logic cannot find a method by the given name, it throws an X::Method::NotFound error. A CATCH phaser can handle such cases, but there is a simpler, more idiomatic way: the FALLBACK method.

The FALLBACK Method

When a class provides a FALLBACK method (directly, via inheritance, or through a role), that method is called whenever a method cannot be found. The missing method’s name is passed as the first argument, and all other arguments are passed verbatim.

Simple Example

class B {
    method FALLBACK($name) { $name }
}

say B.foo;      # foo
say B.bar(42);  # Too many positionals passed; expected 2 arguments but got 3

Multi‑method FALLBACK

class C {
    multi method FALLBACK($name) {
        $name
    }
    multi method FALLBACK($name, $value) {
        "$value.raku() passed to '$name'"
    }
}

say C.foo;      # foo
say C.bar(42);  # 42 passed to 'bar'

Ignoring Arguments

If you don’t care about any arguments and just want to return Nil, you can use a nameless capture (|) as the signature:

class D {
    method FALLBACK(|) { Nil }
}

say D.foo;      # Nil
say D.bar(42);  # Nil

Using FALLBACK to Correct Underscore/Hyphen Mistakes

Raku allows hyphens in identifier names (kebab case). Programmers coming from other languages often mistakenly use underscores (snake case), which leads to runtime errors. A FALLBACK method can intercept these errors and correct them.

class E {
    method foo-bar() { "foobar" }

    method FALLBACK($name, |c) {
        my $corrected = $name.trans("_" => "-");
        if self.^find_method($corrected, :no_fallback) -> &code {
            code(self, |c)
        }
        else {
            X::Method::NotFound.new(
                :invocant(self), :method($name), :typename(self.^name)
            ).throw;
        }
    }
}
  • foo-bar is a valid method.
  • The FALLBACK method receives the missing method name ($name) and any additional arguments (|c).
  • It replaces underscores with hyphens, looks up the corrected name using .^find_method with :no_fallback (to avoid recursive fallback), and, if found, invokes the method with the original arguments.
  • If no corrected method exists, it re‑throws a Method::NotFound error.
say E.foo-bar;  # foobar
say E.foo_bar;  # foobar

A Reusable Role: Method::Misspelt

The logic above can be packaged into a role and distributed as a module. This led to the creation of the Method::Misspelt role, which provides the same functionality along with additional features and optimizations for multiple classes that consume the role. See the module’s internal documentation for details.

Conclusion

This episode introduced the FALLBACK method, demonstrated several ways to customize it, and showed a practical use case for correcting underscore‑to‑hyphen mistakes. A bonus module, Method::Misspelt, was created as a result of this blog post.

Stay tuned for the next episode!

0 views
Back to Blog

Related posts

Read more »

Store Proxy Fetch

Cases of UPPER – Part 12 Containers in Raku This is part 12 of the Cases of UPPER series, which documents the Raku syntax elements that are written completely...