Function definition/call syntax: parameter=value
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposte (2)
Walter Roberson
il 14 Feb 2018
No.
1 Commento
Walter Roberson
il 14 Feb 2018
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
il 14 Feb 2018
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 Commenti
Walter Roberson
il 15 Feb 2018
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
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!