Below are some thoughts, syntax mainly, how it could look like.
leo
Proposal: Named Arguments
pdd03 is already mentioning named arguments, but a concrete syntax
is still missing, as well as how it could work.
First a snippet of the proposed syntax:
.sub named_test :main
.local pmc a, b, c, h
a = new .Integer
a = 1
b = new .Integer
b = 2
b = new .Integer
c = 3
# all these calls do the same
foo(a, "c" => c, "b" => b)
bar(a, c :named('c'), b :named('b'))
baz(a, "c" => c, "b" => b)
h = new .Hash
h['c'] = c
h['b'] = b
foo(a, h :named :flat) # **kw
.end
.sub foo
.param pmc a # 1
.param pmc berta :named('b') # 2
.param pmc 'c' => c # 3
.end
.sub bar
.param pmc a
.param pmc berta :named('b') # 2
.param pmc h :named :slurpy # slurpy hash **kw / %_
# { 'c' => 3 }
.end
.sub baz
.param pmc a
.param pmc d :optional # positionell
.param int has_d :opt_flag # 0
.param pmc berta :named('b') # 2
.param pmc c :named('c') # 3
.param pmc e :named('e') :optional # Null
.param int has_e :opt_flag # 0
.end
Some remarks:
- 'name' => var is the same as: var :named('name')
(we can implement one of these or both)
- the semantics are roughly these of Python 5.3.4 Calls
http://docs.python.org/ref/calls.html
More detailed semantics will probably emanate from a reference
implementation.
At argument opcodes level, a named argument are 2 items: name, var,
where the String 'name' is marked with the :named bit, e.g.:
set_args '(0, 0x80, 0, 0x80, 0)', a, 'c', c, 'b', b
A keyword or slurpy hash is just a pmc argument with both the
:flat/:slurpy bit and the :named bit set.
set_args '(0, 0x88)', a, h
Comments welcome,
leo