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

Map Access Guide

Map Access

Feature Overview

This feature adds array access support to maps.

Prerequisite

None

Sweet Apex Example

public class MapAccess {
    public static void main() {
        Map count = new Map{ 'a' => 2 };
        Integer i = count['a'];
        ++count['a'];
        count['a']++;
        count['a'] += 2;

        List nums = new List{ 2 };
        Integer j = nums[0];
        ++nums[0];
        nums[0]++;
        nums[0] += 2;
    }
}

Transpiled Apex

public class MapAccess {
    public static void main() {
        Map count = new Map{ 'a' => 2 };
        Integer i = count.get('a');
        count.put('a', count.get('a') + 1);
        count.put('a', count.get('a') + 1);
        count.put('a', count.get('a') + 2);
        List < Integer > nums = new List{ 2 };
        Integer j = nums[0];
        ++nums[0];
        nums[0]++;
        nums[0] += 2;
    }
}

Usage

Array access for maps support assignment expressions, postfix expressions, and prefix expressions.

Postfix and prefix expressions of map access cannot be used in assignments.

Contribute on Github! Edit this section.