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

Var Guide

Var

Feature Overview

This feature infers variable types according to the context.

Prerequisite

None

Sweet Apex Example

public class VarDemo {
    public static void main() {
        var t = 'String';
        var size = t.length();
        var str = t.toString();

        var acc = new Account();

        String [] slist = { 'a' };
        var ref = slist;
    }
}

Transpiled Apex

public class VarDemo {
    public static void main() {
        String t = 'String';
        Integer size = t.length();
        String str = t.toString();

        Account acc = new Account();

        List slist = { 'a' };
        List ref = slist;
    }
}

Usage

This feature uses the typing information to infer the variable type.

Currently, classes under namespace System have typings imported by default.

Typing information in source directory will be scanned by default.

Configure scanDestDir to scan the destination directory to collect the typing information.

If you want to add any extra directory to scan, add it in classpath, separated by : if there are multiple values.

Exceptions will be thrown if it fails to infer the correct type.

Contribute on Github! Edit this section.