[nem-en] Extension methods (was Re: My thoughts on Nemerle)

Alejandro Serrano trupill at yahoo.es
Mon Jan 9 21:22:40 CET 2006


Kamil Skalski escribió:

>A nice discussion about extension methods
>http://www.interact-sw.co.uk/iangblog/2005/09/26/extensionmethods
>(also with some critique)
>  
>
Well, I think we are mostly discussing extension methods for 
compatibility with C#. In my point of view, they don't introduce much to 
the language, except for the way System.Query is designed.

>>Well, the reason underneath the use of only [Extension] in methods is
>>that it would not require any modification to the compiler, nor any
>>macro. Indeed, Microsoft C# Compiler, just translates
>>    
>>
>
>I guess the simplest solution here is to create
>
>namespace Nemerle {
>  [Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers,    
>Nemerle.MacroTargets.Method)]
>  macro Extension (tb : TypeBuilder, mb : MethodBuilder)
>  {
>      // take first parameter and extend its type
>         ....
>      // mark method as System.CompilerServices.ExtensionAttribute
>      mb.AddCustomAttribute (<[ System.CompilerServices.ExtensionAttribute ]>);
>  }
>}
>
>This will mimick the VB behavoiur
>
>and additonally
>
>  [Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers,
>Nemerle.MacroTargets.Parameter)]
>  macro Extension (tb : TypeBuilder, mb : MethodBuilder, p : ParameterBuilder)
>  syntax ("this")
>  {
>    // do the same
>  }
>
>which will mimick C# behaviour (also the 'this' syntax at parameter).
>
>Also it would be possible to detect extension method directly from presence of
>System.CompilerServices.ExtensionAttribute specified by user, but I
>see no problem in using only our macro Nemerle.Extension.
>  
>
Indeed I think this is enough, because we don't need to know the type to 
use the attribute.
Of course, I don't know very much about macros, so maybe extends its 
type means something different than I'm thinking now. And we should also 
check that only static methods get this attribute.

>  
>
>>About how to include extension methods, after all this discussion, I've
>>seen we cannot be sure that only a class will be used for extension
>>methods, so I think the best way to go is to allow both "using X" on
>>classes and namespaces.
>>    
>>
>
>Right. Everybody should use cleaner way of opening extension methods
>(open only the class it is contained in) where possible and just open
>entire namespace if there are too many classes to open manually.
>
>The only nontrivial part left is 'extend its type' part of my macros
>implementation ;-)
>  
>
I attach a C# implementation of System.Query to start thinking about 
query-like expressions. I think support as in C# would be fine. Also, I 
remember someone asked why it is:

from c in costumers
where c.Name == "a"
select c;

That is because then it is easy to translate as

costumers.Where (_.Name == "a").Select(_)

P.D.: When will Nemerle 1.0 come...? ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: System.Query.zip
Type: application/x-zip
Size: 53264 bytes
Desc: not available
Url : /mailman/pipermail/devel-en/attachments/20060109/d444d3d6/System.Query-0001.bin


More information about the devel-en mailing list