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

Function Guide

Function

Feature Overview

This feature converts a static method to a Func.

For more details on Funcs, please check R.apex.

Prerequisite

You need to include R.apex if you want to enable this feature.

Sweet Apex Example

public class FunctionDemo {
    /**
     * A sample add function
     * */
    @func
    public static Integer add(Integer a, Integer b) {
        return a + b;
    }

    public static void test() {
        System.debug(FuctionDemo.F.add.run(1, 2));
    }
}

Transpiled Apex

public class FunctionDemo {
    /**
     * A sample add function
     * */
    public static Integer add(Integer a, Integer b) {
        return (Integer)F.add.runN(new List{ a, b });
    }
    public static void test() {
        System.debug(FuctionDemo.F.add.run(1, 2));
    }

    public static final Funcs F = new Funcs();

    public class Funcs {
        public Func add = new AddFunc();
    }

    private class AddFunc extends Func {
        public AddFunc() {
            super(2);
        }
        public override Object execN(List args) {
            Integer a = args.get(0) == null ? null : (Integer)args.get(0);
            Integer b = args.get(1) == null ? null : (Integer)args.get(1);

            return a + b;
        }
    }
}

Usage

@func works only with static methods.

To use the generated Funcs, you will have to refer to ClassName.F.funcName.

Contribute on Github! Edit this section.