Specifying optional and default values for method parameters
An embodiment provides a way to handle default parameters in a method call that are not constant values. During the compiling process of a source code method a compiler generates code before the method body, for every optional parameter. The generated code checks if each optional parameter has a valid value. If a known tag is found then the code generated evaluates the default expression and assign the return value to the corresponding parameter. During the compiling process of a source code method call a compiler, or a semantic analyzer, identifies the defined arguments in the method call. If any arguments are missing, the process uses a known tag for the missing argument. Once all parameter have valid values, passed as arguments or returned from the default expression evaluation, then the original method body is executed.
1 . A computer implemented method for managing an undefined parameter in a method call, comprising:
receiving source code;
identifying in the source code a first method call having arguments;
verifying in the method call that all parameters are defined;
if not all the parameters are defined determining if the undefined parameter is required;
if the undefined parameter is not required, indicating that the parameter is optional in the first method call's arguments; and
compiling the first method call.
2 . The method of claim 1 further comprising:
executing the first method call on a computer;
determining if any of the arguments in the first method call were identified as optional;
applying a default argument for each parameter that was identified as optional;
if the default argument for the optional parameter is a second method call, then accessing the second method call associated with default argument; and
inserting a value returned from the second method as the default argument.
3 . The method of claim 2 wherein the second method call is defined in an original method definition.
4 . The method of claim 2 wherein the second method call is defined in the first method call.
5 . The method of claim 1 further comprising:
outputting a compiled method call to a device capable of executing the compiled method call.
6 . The method of claim 1 wherein the step of verifying the parameters occurs within a semantic analyzer.
7 . The method of claim 6 wherein the semantic analyzer is a component of a compiler.
8 . A data structure configured to manage at least one undefined parameter in a method call, comprising:
code that causes a method call to be made;
at least one defined parameter; and
at least one undefined parameter having a default value determined by a method call.
9 . The data structure of claim 8 further comprising:
at least one undefined parameter having a default value defined by a predetermined value.
10 . The data structure of claim 8 further comprising:
a second undefined parameter having a default value determined by a method call.
11 . The data structure of claim 10 wherein the second undefined parameter default value is determined by a second method that is different from the first method.
12 . The data structure of claim 8 wherein the data structure is configured for use in C# programming language.
13 . The data structure of claim 8 wherein the data structure is configured for use in Visual Baisc.NET programming language.
14 . A computer system comprising:
a compiler configured to generate a compiled version of a source code; and
a semantic analyzer configured to identify a missing parameter from a method call that is defined by a default value determined by a second method call.
15 . The computer system of claim 14 wherein the semantic analyzer is a component of the compiler.
16 . The computer system of claim 14 further comprising:
a code generator configured to generate code for the method call and the second method call in a desired language.