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;
    }
}
​x
 
1
public class DefaultValueDemo {
2
    public static Integer init() {
3
        return 0;
4
    }
5
​
6
    public static Integer add(
7
        @defaultValue(init()) Integer a,
8
        Integer b = init()
9
    ) {
10
        return a + b;
11
    }
12
}

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;
    }
}
11
 
1
public class DefaultValueDemo {
2
    public static Integer init() {
3
        return 0;
4
    }
5
    public static Integer add(Integer a, Integer b) {
6
        a = (a == null) ? init() : a;
7
        b = (b == null) ? init() : b;
8
​
9
        return a + b;
10
    }
11
}

Usage

@defaultValue can only be applied to method parameters.

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

Contribute on Github! Edit this section.

  • Feature Overview3 sec read
  • Prerequisite1 sec read
  • Sweet Apex Example10 sec read
  • Transpiled Apex11 sec read
  • Usage5 sec read
    Copy