Code Monkey home page Code Monkey logo

dispatch.m's Introduction

Dispatch.m

Runtime multiple dispatch for Matlab.

  • Dispatch based on the number of arguments
  • Dispatch based on the type of arguments
  • Supported for code generation

Write a function like the following example as a template. Use dispatch(varargin, methodTable) function to invoke methods.

function varargout = foo(varargin)

    methodTable = {@foo1, ["any"];  % dispatch based on number of inputs
    @foo2, ["logical","logical"];   % dispatch based on type
    @foo3, ["numeric", "logical"];
    @foo3, ["logical", "numeric"];  % repeated method for different type
    @foo4, ["Person"];              % dispatch on class
    @foo5, ["any", "logical"]};             

    [varargout{1:nargout}] = dispatch(varargin, methodTable);

end

Wrtie different functions as methods.

function out = foo1(a)
    out = a;
end

function out = foo2(a, b)
   out = logical(a && b);
end

function out = foo3(a, b)
    out = a * b;
end

function [out1,out2] = foo4(p)
    out1 = p.name;
    out2 = p.age;
end

function [out1,out2] = foo5(a,b)
    out1 = a;
    out2 = b;
end

Now let's test the example:

% dispatch based on number of inputs
>> foo(2)
ans =
     2
% dispatch based on type
>> foo(true, false)
ans =
  logical
   0
% dispatch based on type
>> foo(2, true)
ans =
  2
% dispatch on number of output args
>> p = Person("Amin",25);
>> foo(p) % dispatches on foo1
ans = 
  Person with properties:
    name: "Amin"
     age: 25

>> [a,b] = foo(p) % dispatches on foo4
a = 
    "Amin"
b =
    25
% dispatch on any type
>> foo({2},true)
ans =
  logical
   1
% error handling
>> foo({2},p)
error: no method found

Note

  • You can't have multiple outputs for your function. Instead return the outputs as an array or cell of outputs. FIXED by @bellomia, with a soft change in API: the top-level wrapper (foo in the example) has to feature the varargout syntax.

  • You can't dispatch on the name of the structs. Instead define simple class with just properties (See Person).

License

This is written as part of my Master's thesis and it is licensed under Apache V2, so cite this paper if you use it:

A. Yahyaabadi, P. Ferguson, ”An intelligent multi-vehicle drone testbed for space systems and remote sensing verification,” in Canadian Aeronautics and Space Institute (CASI) ASTRO, Canada, 2019

In case of changes, either make pull requests to this repository or state the changes.

dispatch.m's People

Contributors

aminya avatar beddalumia avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

beddalumia

dispatch.m's Issues

Example does not run as advised: probably need to get rid of weak typing in `ismethod`

>> foo(true,2)
Index exceeds the number of array elements (1).

Error in dispatch>isa_ (line 73)
        varisa(i) = isa2(var{i},type{i});

Error in dispatch>ismethod (line 42)
        out = isa_(var, numOrType);

Error in dispatch (line 24)
        if ismethod(var, methodTable{i,2})

Error in foo (line 3)
    out = dispatch(varargin,...

Though it works if I remove the dispatch to the "Person" class in the methodTable. I believe this happens because of the effective weak-typed use of numOrType in ismethod.

On a side note I think you probably could enable multiple outputs by just making use of varargout and collecting from the specialized implementation everything as [varargout{1:nargout}]. This was the initial patch I was targeting, but the fundamental design issue (in my view) with collision between dispatch on number of (numeric-only) and on non-numeric types has made me desist. Nevertheless I find the idea of building an "julia-like" ergonomic interface for multiple dispatch via isa quite interesting, I might return on this when I have time: matlab has all the needed functionality for that, just a painful API. Thanks for the idea :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.