I have just committed basic support for optional arguments!

def foo(a:1)
  puts a
end

foo # => 1
foo(2) # => 2

This compiles to two "foo" methods, shown here:

public static void foo(int);
  Code:
   0:   getstatic       #14; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   iload_0
   4:   invokevirtual   #19; //Method java/io/PrintStream.println:(I)V
   7:   return

public static void foo();
  Code:
   0:   iconst_1
   1:   invokestatic    #24; //Method foo:(I)V
   4:   return

Pretty simple, eh?

Because we are currently using the optional argument syntax in the
parser, you need to wrap any non-literal expressions in a block of
code, as in...

def foo(a:begin; bar; end)
  puts a
end

def bar
  'hello'
end

foo # => 'hello'

I know it was easier to get the : declaration syntax into the parser
by reusing optional arguments, but I think it's going to be a problem
since we can no longer distinguish (easily) between normal declared
required arguments and declared optional arguments. I don't want
people to have to use begin/end to wrap things like this. Thoughts?

I also noticed that one of the other syntaxes doesn't appear to parse
right with the current parser:

def foo(a:String = 'blah')

...complains about a "dynamic constant assignment". I think we're
going to need that syntax to work...

P.S. It was getting late for me here, so I did not write any tests and
I have not added support to the .java compiler. TODO...feel free to
take them on.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to