Wiki

Ticket #347 (closed defect: fixed)

Opened 4 years ago

Last modified 4 years ago

Cannot make use of optional arguments declared in a library

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

Description

The optional argument support in Cobra doesn't work across libraries. In other words, if the method is declared like so:

class X

    def foo(verbose = false) as int
        return if(verbose, 1, 0)   

Then trying to invoke the method without an argument will work in the same code base, but fail in another project with:

error: The method "foo" is expecting 1 argument, but no arguments are being supplied in this call. The declaration is "foo(verbose as bool)"

This all came up because I tried using optional args in Cobra.Core after which some test cases failed the above error.

The solution will almost certainly be the generation and scanning of attributes on the argument. Ideally, we would use the same conventions as the C# compiler for maximum compatibility between Cobra and C#.

Change History

Changed 4 years ago by Charles

Not that we're trying to get things working on .NET < 4, but when I went looking too see what if any attributes the C# compile uses, I came across this:
 http://stackoverflow.com/questions/5116340/c-sharp-3-5-optional-and-defaultvalue-for-parameters

Changed 4 years ago by Charles

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

Changed 4 years ago by Charles

It turns out that .NET has had this feature for a long time and longer than C#. Reflection already covers this:

"""
ParameterInfo
    .isOptional
    .hasDefaultValue
    .defaultValue

    per http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx
"""

use System.Reflection

class X

    def one(a as int, b as int)
        pass
        
    def two(a as int, b as int = 5)
        pass

    def main is shared
        t = X
        for mi in t.getMethods(BindingFlags(Public, Instance, DeclaredOnly))
            trace mi
            for pi in mi.getParameters
                trace pi, pi.hasDefaultValue, pi.isOptional
                if pi.hasDefaultValue, trace pi.defaultValue

Changed 4 years ago by Charles

  • owner changed from hopscc to Charles

I got this.

Changed 4 years ago by Charles

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

Fixed in changeset:3063

Changed 4 years ago by nerdzero

  • status changed from closed to reopened
  • resolution fixed deleted

Careful, I think .hasDefaultValue is from 4.5

Changed 4 years ago by Charles

Wow, thanks for the catch!

Changed 4 years ago by Charles

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

Dependency removed. My external test still works. Ran whole test suite.

changeset:3065

Note: See TracTickets for help on using tickets.