Creating JVM language [PART 11] - Default parameters

Sources

Java and default parameters

The absence of default parameters is one of the things I always hated in java.
Some people suggest using builder pattern but this solution leads to lots of boilerplate code.
I have no idea why java team have neglected this feature for so long.
It actually turns out that it is not that hard to implement.

Argument vs Parameter

Those terms are often mixed but actually they have different meaning.
The simples way to put it is:

parameter - method signature

argument - method call

An argument is an expression passed when calling the method.
A parameter is a variable in method signature.

Concept

The idea is to look up method signature during call and get the parameter’s default value from it.
That way no modifications regarding bytecode are required. The function call just “simulates”
a default value as if it was passed explicitly.

Grammar changes

The function parameter consists of type, followed by name. Optionally ( ‘?’ ) it is followed
by equals sign (‘=’) followed by some by default expression.

Mapping antlr context objects

Changes in this section are minor. New field (defaulValue) was introduced into FunctionParameter class.
The field stores Optional<Expression> object. If the parser founds defaultValue then the Optional consits of this value.
Otherwise the Optional is empty.