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

Patch Guide

Patch

Feature Overview

This feature enables patching methods to an existing type.

Prerequisite

None.

Sweet Apex Example

public class PatchDemo {
    @patch(String)
    public static String prefix(String s, String prefix) {
        return prefix + s;
    }

    public static void main() {
        String result = 'abc'.prefix('_');
    }
}

Transpiled Apex

public class PatchDemo {
    public static String prefix(String s, String prefix) {
        return prefix + s;
    }
    public static void main() {
        String result = PatchDemo.prefix('abc', '_');
    }
}

Usage

The method annotated with @patch is a patching method.

Patching methods should be public/global static, and they should declare the type to patch on. Besides, the first parameter should accept the instance of the patch class.

Contribute on Github! Edit this section.