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

Lambda Guide

Lambda

Feature Overview

This feature converts a lambda expression to an anonymous Func.

Check details on R.apex.

Prerequisite

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

Sweet Apex Example

public class LambdaDemo {
    public static Func f = (Integer a) -> {
        return a + 1;
    };
}

Transpiled Apex

public class LambdaDemo {
    public static Func f = new AnonymousFunc0(new Sweet.AnonymousContext(null, new Map{  }));

    private class AnonymousFunc0 extends Func {
        private Sweet.AnonymousContext anonymous_context;
        public AnonymousFunc0(Sweet.AnonymousContext context) {
            super(1);
            this.anonymous_context = context;
        }
        public override Object execN(List args) {
            Integer a = args.get(0) == null ? null : (Integer)args.get(0);
            return a + 1;
        }
    }
}

Usage

Lambda expression are like (Type1 name1, Type2 name2) -> { ... }.

We convert lambda expression to anonymous functions.

Here are the things we need to pay attention to:

  • Lambda expressions take this as reference to the enclosing object, not the functions.

  • Nested lambda expressions are supported.

  • You can refer to variables from enclosing block by using outer.Xxx, but this does NOT support nested lambda expressions.

Contribute on Github! Edit this section.