Sweet.apex

  • Docs
  • Tutorials
Docs Menu
  • Sweet.apex Core
    • Transpilation
    • Grammar
    • Command
    • Config
  • Features
    • Action
    • Apex Doc
    • Array Creation
    • Aspect
    • Cast
    • Default Value
    • Enum
    • File
    • Function
    • Identity
    • Injection
    • Lambda
    • Log
    • Mod
    • Not Null
    • Operator
    • Optional
    • Reflection
    • Rethrow
    • Switch
    • Template String
    • Template
    • Script
    • Tagged String
    • Annotation
    • Nullable
    • Var
    • Val
    • Map Access
    • Constructor
    • Transaction
    • Destructure
    • Import Static
    • Pipeline
    • Varargs
    • Patch
    • Import As
  • Plugin Development
    • Feature
    • Test Case

Default Value Guide

Default Value

Feature Overview

This feature sets the default value for method parameters.

Prerequisite

None

Sweet Apex Example

public class DefaultValueDemo {
    public static Integer init() {
        return 0;
    }

    public static Integer add(
        @defaultValue(init()) Integer a,
        Integer b = init()
    ) {
        return a + b;
    }
}

Transpiled Apex

public class DefaultValueDemo {
    public static Integer init() {
        return 0;
    }
    public static Integer add(Integer a, Integer b) {
        a = (a == null) ? init() : a;
        b = (b == null) ? init() : b;

        return a + b;
    }
}

Usage

@defaultValue can only be applied to method parameters.

Or you can append = ... expressions to the parameter name.

Contribute on Github! Edit this section.