Interface ParameterDefaultValueProvider
- All Superinterfaces:
Ordered
- All Known Implementing Classes:
PythonParameterDefaultValueProvider
ParameterElement as an expression the
caller evaluates at the invocation site.
Implementations are discovered with the service loader, so a language module contributes one
without core knowing about the language. Each is asked supports(ParameterElement)
before use, in Ordered order, and the first that supplies an expression wins.
This supports languages that compile a default argument to an accessor the caller invokes.
Scala, for example, compiles class Greeter(greeting: String = "hello") to a single
constructor plus a static $lessinit$greater$default$1() getter, so an absent argument
can be filled in like:
new Greeter(greeting != null ? greeting : Greeter.$lessinit$greater$default$1())
It is deliberately not a general mechanism for default arguments. Languages that
evaluate defaults inside the callee cannot use it — a Kotlin default may reference this
and preceding parameters and exists only within the synthetic $default body (see
KotlinParameterElement), and an interpreted language's default may not be expressible
as bytecode at all. Such languages report ParameterElement.hasDefault() without
contributing a provider, and supply the value by their own means.
- Since:
- 5.2.0
- Author:
- graemerocher
-
Field Summary
Fields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Method Summary
Modifier and TypeMethodDescriptionOptional<io.micronaut.sourcegen.model.ExpressionDef> defaultValueExpression(ParameterElement parameter, @Nullable io.micronaut.sourcegen.model.ExpressionDef target) Supplies an expression evaluating to the parameter's declared default value.booleansupports(ParameterElement parameter) Whether this provider can supply default values for the given parameter, which is typically a test of the element's language specific implementation type.
-
Method Details
-
supports
Whether this provider can supply default values for the given parameter, which is typically a test of the element's language specific implementation type.- Parameters:
parameter- The parameter- Returns:
- True if
defaultValueExpression(ParameterElement, ExpressionDef)should be consulted for this parameter
-
defaultValueExpression
Optional<io.micronaut.sourcegen.model.ExpressionDef> defaultValueExpression(ParameterElement parameter, @Nullable io.micronaut.sourcegen.model.ExpressionDef target) Supplies an expression evaluating to the parameter's declared default value.The returned expression must be assignable to the erasure of
ParameterElement.getType()and must be safe to evaluate at most once per invocation, in argument position. Implementations should be side effect free, as this method may be called more than once for the same parameter.- Parameters:
parameter- The parameter, for whichParameterElement.hasDefault()is truetarget- The receiver of the call the parameter belongs to, ornullwhen the default is obtained without one — as for a constructor, or where the language emits a static accessor- Returns:
- The expression, or
Optional.empty()if the default cannot be materialised at the call site, in which case the caller falls back to the default value of the parameter's type
-