[nem-en] Extension methods (was Re: My thoughts on Nemerle)
Kamil Skalski
kamil.skalski at gmail.com
Mon Jan 9 17:57:12 CET 2006
A nice discussion about extension methods
http://www.interact-sw.co.uk/iangblog/2005/09/26/extensionmethods
(also with some critique)
2006/1/9, Alejandro Serrano <trupill at yahoo.es>:
> I have decided to split the conversation because it was starting to
> become confusing.
>
> > > About specifying "this", the only problem I see is that then, on the
> > > method code, you have to substitute all references of this with the
> > > actual parameters, which I find really painful (and on my point of view,
> > > non-sense)
>
> >
> > Ah, does it mean that this "extension" parameter is refered to as 'this'? Like:
> > module Ex {
> > public ListSplit (this x : string) : list [char] {
> > List.FromArray (this.Split())
> > }
> > }
>
Ok, I checked and "this parameter" is referenced by its name (like
'x') not by mock 'this' reference (which I thought you were saying).
>
> 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.
>
> 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 ;-)
--
Kamil Skalski
http://nazgul.omega.pl
More information about the devel-en
mailing list