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

Optional Guide

Optional

Feature Overview

This feature generates a method with optional parameters.

Prerequisite

None

Sweet Apex Example

public class OptionalDemo {
    public static Integer add(Integer a, @optional Integer b, Integer c?) {
        b = b == null ? 0 : b;
        c = c == null ? 0 : c;

        return a + b + c;
    }
}

Transpiled Apex

public class OptionalDemo {
    public static Integer add(Integer a, Integer b, Integer c) {
        b = b == null ? 0 : b;
        c = c == null ? 0 : c;

        return a + b + c;
    }

    public static Integer add(Integer a, Integer b) {
        return add(a, b, null);
    }

    public static Integer add(Integer a) {
        return add(a, null);
    }
}

Usage

@optional can only be used as the rear parameters in a method.

Or you can append ? to the parameter name.

Contribute on Github! Edit this section.