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

Switch Guide

Switch

Feature Overview

This feature converts switch-case structure to nested if-else.

Prerequisite

None

Sweet Apex Example

public class SwitchDemo {
    public static void test() {
        Integer i = 3;
        switch(i) {
            case 0:
                System.debug('0');
                break;
            case 1:
            case 2:
                System.debug('other');
            default:
                return;
        }
    }
}

Transpiled Apex

public class SwitchDemo {
    public static void test() {
        Integer i = 3;
        Object SwitchDemo_test_s = i;
        if(SwitchDemo_test_s == 0) {
            System.debug('0');
        } else {
            if(SwitchDemo_test_s == 1) {
            }
            if(SwitchDemo_test_s == 2) {
                System.debug('other');
            }
            return;
        }
    }
}

Usage

Contribute on Github! Edit this section.