#27 Method Parameter Defaults

brian Sun 8 Jan 2006

I added end-to-end support for method parameter defaults. The Fan syntax is:

class A
{
  static Int f(Int a := 3) { return a }
}

The FParam entry in the fcode will contain an fcode buffer for the default expression:

--ParamTest0::A extends sys::Obj--
    [SourceFile] len=8
      Memory
  sys::Int f(sys::Int a) [public static]
    [Default: a]
      0: LoadInt       3
    [Code]
      0: LoadVar       0
      3: ReturnObj
  sys::Void make() [public new]
    [Code]
      0: ReturnVoid

Then the Java emit code will generate the appropriate one (or more) wrapper methods:

class A
{
  static Int f() { return f(3); }
  static Int f(Int a) { return a; }
}

Default parameters can be used with constructors, static methods, and instance methods. I have not implemented any error checking to make sure you aren't trying to use invalid expressions (such as accessing a instance variable for a constructor or static method). But since I expect mode defaults will be literals, this should get us pretty far.

Login or Signup to reply.