Function definition/call syntax: parameter=value

9 次查看(过去 30 天)
Hello, I've been using MATLAB for long and understand its advantages. However, one feature that I miss is 'parameter=value' syntax in its function definition/call. I know that inputParser can do wonderful jobs, but '...=...' syntax, that's now offered by many other programming languages like Python and IDL, makes the code simpler and easy understanding. Is there any plan to implement such feature in MATLAB?
Many thanks.

回答(2 个)

Walter Roberson
Walter Roberson 2018-2-14
No.
  1 个评论
Walter Roberson
Walter Roberson 2018-2-14
You would have a higher hope that Mathworks would provide the facility using a different operator such as := or :-
The parser for MATLAB is apparently pretty hacked up, not table driven such as BNF with yacc and lex. Reusing the = operator without messing up the parsing of assignment is not trivial, especially as it is important to be able to catch the common error of failing to close a [] or {} or () before starting a new statement.
If the parser were to be enhanced to be able to reliably diagnose failure to end an expression versus attempt to use a keyword parameter with = then I think it is more likely that Mathworks would choose to instead extend = to allow assignments inside expressions as is common in C and C++ and in Functional Programming. For example people have been demanding a ++ and += operator like in C, but if you are going to allow assignment in the middle of an expression then you need the entire ecosystem of such operations including inline = to mean assignment. Using = for keyword value pairs would lock out that possibility of extension. I would think that it is much more likely that Mathworks is reserving = for inline assignment "eventually" than that they would lock out that option by using = for named options in function calls. If a different operator were to be used for named options then the inline assignment possibility does not get eliminated.
Thus I actively think that Mathworks plans to deny using = for named options.

请先登录,再进行评论。


Steven Lord
Steven Lord 2018-2-14
This sounds like a reasonable enhancement request to file with Technical Support. If you describe how you feel this feature would make your workflow easier or more productive, they can capture that information in the enhancement database for development to consider. You can contact Support using the Contact Us link in the upper-right corner of this page.
  3 个评论
Arindam Chakraborty
编辑:Walter Roberson 2018-2-15
Steve, thanks for your answer and support.
Thanks to Walter too for detailed explanation.
What I wanted really is assignment of default values to parameters in the function definition. For example:
function fun1(x,y=2,z='a')
...
end
At present, there's no way this can be done as one-liner in MATLAB. For such option, '=' is the most reasonable operator. However, use of other operators such as ':=' should also be ok as along as this feature is usable.
It will be useful as well if the help function prints the function definition line as the first line. Thus, help fun1 should print:
%%fun1(x,y=2,z='a')
%%and other help lines the way it does now.
This will again simplify the code and would not require users to write at least few lines even for quick help. Remember that, once default values are assigned in the 1st line, and this line is printed with help, quick look at the usage of a function becomes much more user-friendly.
Thanks.
Walter Roberson
Walter Roberson 2018-2-15
That would be a useful extension -- but I think it would be with a different notation.
There are tensions between positional parameters and named parameters that would have to be resolved. People tend to want to be able to specify named parameters in any order. If someone calls with
fun1(x, 50, y=11)
then is the implication that the 50 should be assigned to y (positional) and then that the y=11 should override the 50? Or should it notice that you passed y with named keyword and so decide the y=2 positional initialization should be skipped in the calling sequence and so decide that the 50 positionally belongs with z ? And should the function be invoked as if the user had called
fun1(x, 11, 50)
or should it be invoked as if the user had called
fun1(x, 'y', 11, 'z', 50)
or should it be treated as
fun1(x, 50, 'y', 11, 'z', 'a') %explicit pairs before default initializations
or as
fun1(x, 50, 'z', 'a', 'y, '11') %explicit pairs after default initialization
or as
fun1(x, 50, 'y', 2, 'z', 'a', 'y', 11) %no searching for overlap with names in what is generated, count on the fact that later name/value pairs are considered to override earlier

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by