|
[
Permlink
| « Hide
]
Arron Washington added a comment - 29/Oct/05 05:27 PM
def Each(func as callable) extends IEnumerable ?
Implementation of my proposed syntax.
The "value" keyword is much better than "self" because it is already implied on properties. "Self" is confusing because you normally don't use it in static methods.
Updated to use "value" instead of "self".
What keyword would you use if support is added for extension properties?
I like either keep using "self" or one of ckknight's proposals: [ExtensionMethod] def Say(s as string): print "you said", s.ToUpper() That makes it easier to convert regular methods to extension methods, too. You would still use "value" in properties. There is no conflict.
extend IDictionary:
def Get(key, default) as object: def GetOrSet(key, default) as object: def Update(d as IDictionary): Selling points in no particular order:
if you have a syntax like "extend IDictionary:" it doesn't translate well between the upcoming C# 3.0. For example, System.Query.Sequence handles most of the LINQ stuff and includes both extension and non-extension methods.
How does that apply? Boo is not C#, and the syntax used to declare extension methods in C# doesn't have to look anything like how C# does it.
well, how would I make a class that contains both extension methods and non-extension methods, then?
I prefer the [ExtensionOf(type)] syntax. You wouldn't. Why would you try to mix instance / static methods and extension methods? How useful is that?
it's done in LINQ.
in the System.Query.Sequence class: Extension Methods: Non-Extension Methods: Though they are primarily extension methods, the non-extension methods do belong in the same place. It's not clear to me why they belong in the same class, why not just the same namespace? But in any case, this is no reason to not use the "extend Foo:" syntax. Consider that we can already do this:
class A: and that "extend Foo:" is meant to parallel "class Foo:". So if we desire extenions inside classes then allow them there too: class Sequence: But don't make me retype IEnumerable 30 times! I also want to point out that since the "extend Foo:" makes the declarations natural, it not only paves the way for extension properties, but also extension to
I also want to point out that since the "extend Foo:" syntax makes the declarations natural, it not only paves the way for extension properties, but also extension fields. And not only in terms of declarations, but also keeping the extension fields private to the extension:
extend Foo: The getting and setting of the field _bar would actually be something like the access of a compiler generated Dictionary<[weak] object, Dictionary<string, object>> which maps objects to extension field names to values. This is just an idea. I'm not proposing that supporting fields be included with BOO-564. I just wanted to show where the "extend Foo:" syntax could take us. I'm dropping the 'self' syntax in favor of the proposals above and to also support extension properties.
The low level way of defining any extension will simply be applying the existing Extension attribute. Where before we did: def DoubleTrouble(self as string): we will now do: [Extension] 'self' will no longer be allowed as a method parameter by the parser. Remember that this is the low level way. The high level way will be implemented as a new type of macro. A type declaration macro (MacroTypeDeclaration ast node) and it will be pretty much the extend syntax in the comment above (although field extension support depends on how good the macro will be). The macro will translate: extend string: To something like the lower level: [Extension] [Extension] which the programmer could have typed in (him|her)self. I think this will give us:
|
||||||||||||||||||||||||||||||||||||||||||||||||