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

Nullable Guide

Nullable

Feature Overview

This feature adds support for nullable variables.

Prerequisite

None

Sweet Apex Example

public class NullableDemo {
    public static void main() {
        String s1 = 'abc';
        Integer i1 = s1.length();

        String s2 = null;
        Integer i2 = s2?.length();

        Integer i3 = ('a' + 'b')?.length();
    }
}

Transpiled Apex

public class NullableDemo {
    public static void main() {
        String s1 = 'abc';
        Integer i1 = s1.length();

        String s2 = null;
        Integer i2 = (s2 != null ? s2.length() : null);

        Integer i3 = (('a' + 'b') != null ? ('a' + 'b').length() : null);
    }
}

Usage

Use nullable variables in method invocations or field accesses.

Try not using nest nullable variables as this will increase complexity.

Contribute on Github! Edit this section.