Ticket #1515 (new bug)

Opened 12 years ago

Last modified 11 years ago

Duplicate named args cause fatal error in subs

Reported by: Austin_Hastings Owned by: whiteknight
Priority: normal Milestone:
Component: none Version: 2.2.0
Severity: medium Keywords:
Cc: jkeenan Language:
Patch status: Platform:

Description

Calling a sub with duplicate named args:

.sub main :main
	foo( 1 :named('a'), 2 :named('a') )
.end

triggers an error:

duplicate named argument in call
current instr.: 'main' pc 0 (test.pir:4)

Unfortunately, this means that setting defaults in a hash, then flattening the hash together with any explicit named args, is no longer a valid way to handle argument defaulting. Given that, either (1) this is a bug, and duplicate named args should be permitted; or (2) this is the official new way, in which case what is the model for arg-defaulting?

Change History

Changed 12 years ago by whiteknight

  • owner set to whiteknight

Are you talking about arg defaulting on the caller side or the callee side? On the callee side, args would default by marking them :optional and autovivifying them with the default value if one is not explicitly provided.

On the caller side, if you're already using and flattening a hash you could check the existence of a particular key in the hash and fill it in if it doesn't currently exist.

Could you provide a longer use-case for this so I can see what you are trying to do?

Changed 11 years ago by jkeenan

  • cc jkeenan added

Austin,

Can you respond to whiteknight or let us know whether this is still an issue?

Thank you very much.

kid51

Changed 11 years ago by coke

Seems like a hash is a potential solution to this:

.sub main :main
    .param pmc opts
    opts = new 'Hash'
    opts['a'] = 1
    opts['a'] = 2
    foo( opts :flat )
.end

.sub foo 
  .param int a :named
  say a
.end
Note: See TracTickets for help on using tickets.