Ticket #325 (closed enhancement: fixed)

Opened 4 years ago

Last modified 4 years ago

Move to .Net 4.0: Support Optional Parameters

Reported by: hopscc Owned by: Chuck
Priority: major Milestone:
Component: Cobra Compiler Version: 0.9.4
Keywords: Cc:


C# .Net 4.0 supports optional parameters (default parameter values) on method calls.
Enhance cobra to do same.
This would allow methods (and hopefully initializers) to be called without multiple convenience overloads being declared.

See  C# 4.0 New Features for C# examples

the obvious Cobra equivalent would be something like

def Process( data as String, ignoreWS as bool = false, moreData as ArrayList? = nil )
    pass # actual work done here

called as

Process( "foo", false, nil )
Process( "foo", false )
Process( "foo" )


def Process( data as String )
    Process( data, false )

def Process( data as String, ignoreWS as Bool )
    Process( data, ignoreWS, nil )

def Process( data as String, ignoreWS as Bool, moreData as ArrayList? )
    pass # Actual work done here

Try and support initializer declaration (removing initializer chaining the same way) - assuming C# supports this on ctors (?)

If you use the same syntax as above (ideally):

  • watch out for complications from handling immediate property assignment in Constructors.
  • watch out for erroneous expression token capture between typing and assignment (see ticket:34)

(an obvious enhancement to this is type inference on optional params since an assignment like form give the possibility of inferring the param type).


optional-params.patch Download (19.2 KB) - added by hopscc 4 years ago.

Change History

  Changed 4 years ago by hopscc

  • owner set to hopscc
  • status changed from new to assigned

Changed 4 years ago by hopscc

  Changed 4 years ago by hopscc

Optional params support methods and initializers.
new tests dir for these tests for methods

Type inference on params supported except where default value is nil.

probably should have fixes on ticket:330 also
working java support needs java-fix path on ticket:275

  Changed 4 years ago by hopscc

  • owner changed from hopscc to Chuck

  Changed 4 years ago by nerdzero

With this patch, how could an IDE extension tell if a Param node was optional? Like this?

if p.optValue <> nil

That doesn't seem like it would work if the default value is nil though.

follow-up: ↓ 6   Changed 4 years ago by hopscc

yep - thats the test the patch uses.
default value nil will have a non Nil optValue.

p.optValue == NilLiteral

Would you prefer a boolean property to examine ??

in reply to: ↑ 5   Changed 4 years ago by nerdzero

Yeah, my initial thought was something like:

if p.isOptional

But the current mechanism would work as well. We just need to be able to tell if a parameter is optional so we can display it correctly in tooltips for code/parameter completion.

  Changed 4 years ago by hopscc


get isOptional as bool 
    return _optValue <> nil 

just after the 'pro optValue' entry in Vars.cobra.

  Changed 4 years ago by Charles

Looking at this now.

  Changed 4 years ago by Charles

  • status changed from assigned to closed
  • resolution set to fixed



If we need further refinements we do additional tickets.

Note: See TracTickets for help on using tickets.