Wiki

Ticket #271 (assigned defect)

Opened 7 years ago

Last modified 7 years ago

Get compile error when using anonymous method with delegate(sig)

Reported by: torial Owned by: Chuck
Priority: major Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: delegate anonymous method Cc:

Description

When I attempt to compile the code attached which uses a sig/delegate and an anonymous method, I get the following compile error:

error: COBRA INTERNAL ERROR / ArgumentOutOfRangeException? / Index was out of range. Must be non-negative and less than the size of the collection.; Parameter name: index
Compilation failed - 1 error, 0 warnings
Not running due to errors above.

Attachments

bug_do.cobra Download (2.0 KB) - added by torial 7 years ago.
File which causes compile error.
generic-method-callchks.patch Download (2.4 KB) - added by hopscc 7 years ago.

Change History

Changed 7 years ago by torial

File which causes compile error.

Changed 7 years ago by hopscc

Its failing cos you're calling a method as a generic when the method itself isnt written to be generic - this is being caught by a require contract in the compiler rather than a test and generate a nice error message.
- Thats probably the first bug that should be corrected

For the code given it will work (compile and generate output) if you change the line

isPrime = .memoize<of int,bool>(ref _isPrimeFast)

to

isPrime = .memoize(ref _isPrimeFast)

i.e use the declared non genericised method

Now the url reference describes a genericised version of Function, .memoize and
the call to it.
A translation to cobra woulld look something like this

    sig Function<of T1, T2>(param as T1) as T2    

    def memoize<of T1, T2>(func as Function<of T1, T2>) as Function<of T1, T2>
        cache = Dictionary<of T1, T2>()
        return do(arg as T1)
            result = false
            if cache.containsKey(arg)
                result = cache[arg]
            else
                result = cache[arg] = func(arg)
            return result
# ...
        isPrime = .memoize<of int,bool>(ref _isPrimeFast) #orig

That should work as expected but
Unfortunately that wont compile in the current compiler because (initially at least) the parsing of sig doesnt recognise/handle generic declarations.

This line

sig Function<of T1, T2>(param as T1) as T2    

generates a compilation error

bug_do.cobra(13,6): error: Encountered OPEN_GENERIC when expecting an identifier.

Thats the second bug/feature enhancement.
There may well be additional hurdles once that is corrected

I'll make a patch for the first bug and generate a ticket for the second.

Changed 7 years ago by hopscc

Changed 7 years ago by hopscc

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

Patch for additional chks and error output to avoid preconditions causing internal errors on invalid generic-method calls.
2 new tests.

Note: See TracTickets for help on using tickets.