项目作者: aminya

项目描述 :
Runtime multiple dispatch for Matlab.
高级语言: MATLAB
项目地址: git://github.com/aminya/Dispatch.m.git
创建时间: 2020-01-22T05:40:32Z
项目社区:https://github.com/aminya/Dispatch.m

开源协议:Other

下载


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.

  1. function varargout = foo(varargin)
  2. methodTable = {@foo1, ["any"]; % dispatch based on number of inputs
  3. @foo2, ["logical","logical"]; % dispatch based on type
  4. @foo3, ["numeric", "logical"];
  5. @foo3, ["logical", "numeric"]; % repeated method for different type
  6. @foo4, ["Person"]; % dispatch on class
  7. @foo5, ["any", "logical"]};
  8. [varargout{1:nargout}] = dispatch(varargin, methodTable);
  9. end

Wrtie different functions as methods.

  1. function out = foo1(a)
  2. out = a;
  3. end
  4. function out = foo2(a, b)
  5. out = logical(a && b);
  6. end
  7. function out = foo3(a, b)
  8. out = a * b;
  9. end
  10. function [out1,out2] = foo4(p)
  11. out1 = p.name;
  12. out2 = p.age;
  13. end
  14. function [out1,out2] = foo5(a,b)
  15. out1 = a;
  16. out2 = b;
  17. end

Now let’s test the example:

  1. % dispatch based on number of inputs
  2. >> foo(2)
  3. ans =
  4. 2
  1. % dispatch based on type
  2. >> foo(true, false)
  3. ans =
  4. logical
  5. 0
  1. % dispatch based on type
  2. >> foo(2, true)
  3. ans =
  4. 2
  1. % dispatch on number of output args
  2. >> p = Person("Amin",25);
  3. >> foo(p) % dispatches on foo1
  4. ans =
  5. Person with properties:
  6. name: "Amin"
  7. age: 25
  8. >> [a,b] = foo(p) % dispatches on foo4
  9. a =
  10. "Amin"
  11. b =
  12. 25
  1. % dispatch on any type
  2. >> foo({2},true)
  3. ans =
  4. logical
  5. 1
  1. % error handling
  2. >> foo({2},p)
  3. 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:

  1. A. Yahyaabadi, P. Ferguson, An intelligent multi-vehicle drone testbed for space systems and remote sensing verication,” 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.