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

Cast Guide

Cast

Feature Overview

This feature casts between different collections of lists, sets and maps.

Prerequisite

None

Sweet Apex Example

public class CastDemo {
    public static void main() {
        List list1 = (List => List)new List();

        Map map1 = (Map => Map)new Map();
    }
}

Transpiled Apex

public class CastDemo {
    public static void main() {
        List list1 = cast_Utils.cast_List_Object_to_List_String(new List());

        Map map1 = cast_Utils.cast_Map_Object_Object_to_Map_String_String(new Map());
    }
}

public class cast_Utils {
    
    public static List cast_List_Object_to_List_String(Object other) {
        List target = (List)other;
        List ret = new List();
        for(Object i : target) {
            String r = (String)i;
            ret.add(r);
        }

        return ret;
    }

    public static Map cast_Map_Object_Object_to_Map_String_String(Object other) {
        Map target = (Map)other;
        Map ret = new Map();
        for(Object key : target.keySet()) {
            Object value = target.get(key);
            String k = (String)key;
            String v = (String)value;
            ret.put(k, v);
        }

        return ret;
    }
}

Usage

An extra cast_Utils.cls will be generated to contain all the casting methods should it be used by other files.

Nested casting like (List> => List>) is also supported.

Contribute on Github! Edit this section.