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

Not Null Guide

Not Null

Feature Overview

This feature guards that method parameters cannot be null.

Prerequisite

None

Sweet Apex Example

public class NotNullDemo {
    public static Integer add(
        @notNull Integer a,
        Integer b!
    ) {
        return a + b;
    }
}

Transpiled Apex

public class NotNullDemo {
    public static Integer add(Integer a, Integer b) {
        Sweet.assertNotNull(a, '"a" in NotNullDemo.add(Integer, Integer) should not be null');
        Sweet.assertNotNull(b, '"b" in NotNullDemo.add(Integer, Integer) should not be null');

        return a + b;
    }
}

Usage

@notNull can only be used on method parameters.

Or you can append ! after the parameter variable.

Contribute on Github! Edit this section.