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

Operator Guide

Operator

Feature Overview

This feature converts a static method to an operator.

Prerequisite

None

Sweet Apex Example

public class OperatorDemo {
    @operator
    public static Integer add(Integer a, Integer b) {
        return a + b;
    }

    public static void main() {
        Object a = 1;
        Object b = 2;
        System.debug(a add b);
    }
}

Transpiled Apex

public class OperatorDemo {
    public static Integer add(Integer a, Integer b) {
        return a + b;
    }
    public static void main() {
        Object a = 1;
        Object b = 2;
        System.debug((Integer)OperatorDemo.add((Integer)a, (Integer)b));
    }
}

Usage

@operator can only be used on public/global static methods with two parameters and one return value.

Some variations are:

ExampleDescription
@operatorConvert the method to an operator
@operator('name')Convert the method to an operator with the given name
@operator(name='name')Convert the method to an operator with the given name

Contribute on Github! Edit this section.