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

Val Guide

Val

Feature Overview

This feature infers variable types according to the context and make it final.

Prerequisite

None

Sweet Apex Example

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

        val acc = new Account();

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

Transpiled Apex

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

        final Account acc = new Account();

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

Usage

Refer to feature var for more details.

Contribute on Github! Edit this section.