Wednesday, December 21, 2005

Extension syntax for Boo

I was thinking about the proposals for expanded extensions for Boo -- I would rather call them mixins, since this is what they seem like in my mind, with their syntactic sugar. Its also easier to type mixin.

I've been thinking about properties and mixins. Take the following example, with my completely made-up syntax.

mixin System.String:
Words as (string):
return self.Split(' ', ';', '.', '!') #Doesn't work, no implict conversion to char

As far as code goes, its pretty simple. Infact, this could easily be a method.

def Words(target as string):
return self.Split(' ', ';', '.', '!')

Then you, know, Words("Hello, world!") is just as effective as "Hello, world!".Words.

But sometimes, being able to do something one way doesn't necessarily mean its better than doing it another way. This kind of syntax blends very well with the way of the OOP, and allows for a better visual recognition of what object is being manipulated.

The flip side is, of course, "Is Words part of System.String, or an extension?"

My answer is, "Does it matter?" Do you really need to know wheither its an external function or an instance function? Well, that would be my answer in an ideal system, where extensions showed up when you used reflection on a type. Right now, they don't, though, so knowing the difference between an extension and instance members is important if you plan on doing reflection-based operations on a type at runtime - you'll have to root around in some namespaces for those extensions rather than just checking the members of a type.

No comments: