How to resolve the algorithm Respond to an unknown method call step by step in the Io programming language

Published on 12 May 2024 09:40 PM
#Io

How to resolve the algorithm Respond to an unknown method call step by step in the Io programming language

Table of Contents

Problem Statement

Demonstrate how to make the object respond (sensibly/usefully) to an invocation of a method on it that it does not support through its class definitions. Note that this is not the same as just invoking a defined method whose name is given dynamically; the method named at the point of invocation must not be defined. This task is intended only for object systems that use a dynamic dispatch mechanism without static checking.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Respond to an unknown method call step by step in the Io programming language

Source code in the io programming language

Example := Object clone do(
    foo := method(writeln("this is foo"))
    bar := method(writeln("this is bar"))
    forward := method(
        writeln("tried to handle unknown method ",call message name)
        if( call hasArgs,
            writeln("it had arguments: ",call evalArgs)
        )
    )
)

example := Example clone
 
example foo          // prints "this is foo"
example bar          // prints "this is bar"
example grill        // prints "tried to handle unknown method grill"
example ding("dong") // prints "tried to handle unknown method ding"
                     // prints "it had arguments: list("dong")"


  

You may also check:How to resolve the algorithm Accumulator factory step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Prolog programming language
You may also check:How to resolve the algorithm Jordan-Pólya numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Prolog programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Mirah programming language