« Previous -
Version 6/7
(diff) -
Next » -
Current version
Daniel Berger, 05/02/2009 03:11 AM
Method Annotations¶
Method annotations in Sapphire are an amalgam of ideas taken from languages like Fortress and Python. They allow programmers to add metadata to a method definition without affecting its behavior.
[WARNING: The following code may affect behavior, too. I haven't decided.]
Type Annotations¶
The following method definition appears to be a form of static typing but is, in fact, a method annotation:
def some_method(String x, Integer y) ... end
How is this useful? First, it provides additional information about the method that can be later retrieved by the (hypothetical) Method#signature method.
m = method(:some_method) m.signature # [String, Integer]
Second, it provides a way for documentation systems such as RDoc to document the expected type of any given argument.
Return Type Annotations¶
An expected return type may also be added to the method definition:
def some_method(x, y) => Fixnum ... end
This information could be retrieved by the (hypothetical) Method#return_type method. It could also be used by documentation systems like RDoc to document the expected return type.
m = method(:some_method) m.return_type # Fixnum
Putting it all together¶
Here's an example that includes type annotations, default values and a return type.
def some_method(String x = 'hello', Integer y = 7) => Fixnum ... end
This still looks fairly clean IMHO.
Other Possible Benefits¶
This could be used to optimize methods, too, along the same lines as DRuby or Duby.