syntax Overriding default gist method

Documentation for syntax Overriding default gist method assembled from the following types:

language documentation Classes and objects

From Classes and objects

(Classes and objects) Overriding default gist method

Some classes might need its own version of gist, which overrides the terse way it is printed when called to provide a default representation of the class. For instance, exceptions might want to write just the payload and not the full object so that it is clearer what to see what's happened. However, this isn't limited to exceptions; you can do that with every class:

class Cook {
    has @.utensils  is rw;
    has @.cookbooks is rw;
 
    method cook$food ) {
        return "Cooking $food";
    }
 
    method clean_utensils {
        return "Cleaning $_" for @.utensils;
    }
 
    multi method gist(Cook:U:{ '' ~ self.^name ~ '' }
    multi method gist(Cook:D:{
        '⚗ Cooks with ' ~ @.utensils.join" ‣ "~ ' using '
          ~ @.cookbooks.map"«" ~ * ~ "»").join" and "}
}
 
my $cook = Cook.new(
    utensils => <spoon ladle knife pan>,
    cookbooks => ['Cooking for geeks','The French Chef Cookbook']);
 
say Cook.gist# OUTPUT: «⚗Cook⚗» 
say $cook.gist# OUTPUT: «⚗ Cooks with spoon ‣ ladle ‣ knife ‣ pan using «Cooking for geeks» and «The French Chef Cookbook»␤» 

Usually you will want to define two methods, one for the class and another for class instances; in this case, the class method uses the alembic symbol, and the instance method, defined below it, aggregates the data we have on the cook to show it in a narrative way.