The principle is to get rid of as much noise as possible. E.g. say you create a LispFunction which should take a number (int/double) and a string. You spend most of that function on converting & testing the arguments (if you're using exceptions or not). E.g.
Code:
[LispFunction ("myfunction1")]
public object myfunction1(ResultBuffer arguments) {
try {
TypedValue[] args = arguments.AsArray();
if (args.Length != 2) throw new LispArgumentCountException(2, args.Length);
if (!((args[0].TypeCode == (int)LispDataType.Int16) ||
(args[0].TypeCode == (int)LispDataType.Int32) ||
(args[0].TypeCode == (int)LispDataType.Double)))
throw new LispArgumentException("Expecting int/double", args[0]);
if (args[1].TypeCode != (int)LispDataType.Text)
throw new LispArgumentException("Expecting string", args[1]);
double myNumber = (double)args[0].Value; // Get the actual value of the 1st argument
string myText = (string)args[1].Value; // Get the actual value of the 2nd argument
// Do some actual stuff
return "success";
} catch (LispException e) {
return e.Message;
}
}
The principle would be to have a handler class which would perform the tests and convert the TypedValues for you. Then you'd use it like this:
Code:
[LispFunction ("myfunction2")]
public object myfunction2(ResultBuffer arguments) {
try {
LispFunctionArgumentHandler args = new LispFunctionArgumentHandler
(arguments, new string[]{"Int16|Int32|Double", "Text"});
double myNumber = (double)args[0]; // Get the actual value of the 1st argument
string myText = (string)args[1]; // Get the actual value of the 2nd argument
// Do some actual stuff
return "success";
} catch (LispException e) {
return e.Message;
}
}
See how the type checking is reduced to a single instruction? What would be nice is if such handler would use something like generics to do the conversion as well - thus the get items would be something like this:
Code:
double myNumber = args[0]; // Get the actual value of the 1st argument
string myText = args[1]; // Get the actual value of the 2nd argument
Or I could do something using argument overloads, but I think the use thereof would be less succinct:
Code:
double myNumber;
args.Get(0, out myNumber);
string myText;
args.Get(1, out myText);