tech

[English] [Japanese]

10.3. Omitting Arguments

LuneScript can omit nilable type arguments when calling functions.

For example, the following function func() can be called without arguments.

// @lnsFront: ok
fn func( val1:int!, val2:int! ):int {
   when! val1, val2 {
      return val1 + val2;
   }
   when! val1 {
      return val1;
   }
   when! val2 {
      return val2;
   }
   return 0;
}
print( func(1,2) );  // 3
print( func(1) );    // 1
print( func() );     // 0

The problem here is that func(1) and func() cannot determine whether they intentionally omitted the argument or forgot to specify it.

Being able to omit arguments is itself a useful feature that saves coding effort, but it may not be possible to detect when you forget to specify an argument that should be specified.

To avoid this, you can prevent forgetting to specify by defining the argument as follows and prohibiting the omission of the argument.

  • Do not use arguments of nilable type
  • Defining a non-nilable type argument after a nilable type argument

However, if the omission of arguments itself is prohibited, the following problems arise.

  • It takes more time to code.
  • Useless argument specification increases and code readability deteriorates.

To avoid the problem of forgetting to specify arguments while omitting arguments, LuneScript introduces explicit argument elision.

Explicit argument omission

LuneScript uses ## if you omit the argument.

When calling func() above:

// @lnsFront: skip
print( func(1,2) );  // 3
print( func(1##) );  // 1
print( func(##) );   // 0

By adding ## to the part where the argument is omitted, it is clarified that the argument is omitted.

If you omit ## without specifying it, it will be a warning.

This prevents omission of arguments and forgetting to specify arguments.

Treat warnings as errors by adding the -Werror option when transcompiling.

On the other hand, if specifying ## is troublesome, add –disable-checking-define-abbr option to suppress warning when omitting ## without specifying it.