Mixing name/value pair syntax

조회 수: 105 (최근 30일)
Matt J
Matt J 2026년 1월 7일 5:56
편집: Matt J 2026년 1월 9일 17:43
In a Matlab seminar a few years ago, I asked why function calls that mix old style and new style name/value pair syntax would only work in a certain order. Specifically, the old style pairs must come first in the argument list. When the order is reversed, an error is thrown as illustrated below.
testFunc('A',10, B=20)
A: 10 B: 20
testFunc(B=20, 'A',10)
Unsupported use of the '=' operator. To compare values for equality, use '=='. To pass name-value arguments using the name=value format, provide these arguments after all other inputs.
A MathWorker at the seminar told me that this was by design, but there was no time for him to elaborate. Can anyone think why this would have been a deliberate design choice?
function testFunc(opts)
arguments
opts.A=1;
opts.B=2;
end
disp(opts)
end
  댓글 수: 12
Rik
Rik 2026년 1월 9일 17:07
Isn't there a single parser that looks for syntax errors? Why would the naive approach of making the Name=Value syntax work the same as {:} cause any issues anywhere? And why there be a requirement for Name=Value to be last either way?
Only partially on topic: I think the monstrosity below shows how poorly I understand what is happening.
tryme(A=10)
A = "A"
B = 10
function tryme(A,B)
arguments
A=1;
B=2;
end
A
B
end
Matt J
Matt J 2026년 1월 9일 17:38
편집: Matt J 2026년 1월 9일 17:43
I think the monstrosity below shows how poorly I understand what is happening.
Nothing monstrous there. As you noted earlier, the A=10 syntax gets converted to a comma-separated pair {"A",10} and so the call is equivalent to,
args={"A",10};
tryme(args{:})
I might have expected,
args = {'A',10}
so that's a little strange.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by