Forums

Generic method constraints; 'sealed' keyword

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Generic method constraints; 'sealed' keyword

Postby gogobyte » Wed Oct 22, 2008 10:27 am

Hello Chuck,

I can't see generic method constraints and 'sealed' keyword implemented in Cobra. Why is that?
gogobyte
 
Posts: 13

Re: Generic method constraints; 'sealed' keyword

Postby Charles » Wed Oct 22, 2008 12:29 pm

I haven't bothered to implement "sealed". What would one due with it besides lock down the system String class? I mean, I know technically what it does, but I've never needed it in any project.

Generic method constraints use the syntax "where X must be Y" so knowing that I did a search:

659 ~/Projects/Cobra/Workspace-New $ gco where | grep must
./HowTo/220-UseDynamicTyping.cobra:51: # - more brittle (to change program to `float` you must find and replace everywhere)
./HowTo/310-IterateThroughRecursiveDataWithYield.cobra:14: where T must be IComparable<of T>
./HowTo/310-IterateThroughRecursiveDataWithYield.cobra:55: where T must be IComparable<of T>
./SamplesInProgress/WebSearcher.cobra:33: where TSearchResult must be SearchResult
./Source/Container.cobra:49: where TMember must be class, IMember
./Source/SharpGenerator.cobra:1274: # could it be a subclass? no. value types are structs and cannot be subclassed so this must be `if i` where `i` is nullable struct
./Source/SharpGenerator.cobra:1428: # could it be a subclass? no. value types are structs and cannot be subclassed so this must be `if i` where `i` is nullable struct
./Tests/240-generics/300-declare-generic-classes/110-generic-class-inheritance-and-more.cobra:4: where T must be IA
./Tests/240-generics/300-declare-generic-classes/130-constructed-class-inheritance-with-for-loop-inference.cobra:18: where T must be IProcessor
./Tests/240-generics/300-declare-generic-classes/600-generic-class-with-type-constraint.cobra:2: where TBar must be Bar
./Tests/240-generics/300-declare-generic-classes/602-instantiate-generic-arg.cobra:2: where TArg must be class, callable
./Tests/240-generics/300-declare-generic-classes/602-instantiate-generic-arg.cobra:13: where TBaz must be Baz, callable
./Tests/240-generics/300-declare-generic-classes/602-instantiate-generic-arg.cobra:30: where TInter must be Inter
./Tests/240-generics/300-declare-generic-classes/610-generic-type-compatibility.cobra:5: where T must be IComparable<of T>
./Tests/240-generics/300-declare-generic-classes/610-generic-type-compatibility.cobra:33: where T must be IComparable<of T>
./Tests/240-generics/300-declare-generic-classes/800-generic-twister.cobra:11: where T must be IAMember

... so publicly see How To: IterateThroughRecursiveDataWithYield.

If you have the workspace there are some additional files above.

The general syntax is:

where <generic-param> must be <constraints>

<constraints> := <constraint> [, <constraint>]*

<constraint> := class | struct | callable | <type>

-Chuck
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Generic method constraints; 'sealed' keyword

Postby gogobyte » Wed Oct 22, 2008 3:17 pm

I mean methods can have generic parameters, but this line: '.genericConstraints(token, params)' is missing from 'classDecl' method.
gogobyte
 
Posts: 13

Re: Generic method constraints; 'sealed' keyword

Postby Charles » Wed Oct 22, 2008 3:25 pm

I see. Yes I haven't gotten to implementing them yet! Sorry.

As a work around, you might use the dynamic or Object types and then check for inheritance:
def foo(x) as dynamic
if x inherits IComparable
pass
else
pass
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Generic method constraints; 'sealed' keyword

Postby Charles » Mon Oct 27, 2008 9:05 pm

I have implemented generic constraints for methods in the repository. Let me know if you have any problems.

You can read the test cases for generic methods at trunk/Tests/240-generics/400-generic-methods.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 115 guests

cron