JADEx Update (v0.42): Readonly Replaces Immutability Based on Community Feedback (github.com)
from justicecoder@programming.dev to programming@programming.dev on 04 Mar 14:39
https://programming.dev/post/46673805

In the previous post, JADEx introduced a new feature Immutability.
Through community feedback, several confusions and limitations were identified.

In v0.42, we have addressed these issues and improved the feature. This post explains the key improvements and new additions in this release.


Improvements

apply immutability -> apply readonly


Expanded Scope of final keyword: now includes method parameters


Example Code

JADEx Source Code

package jadex.example;

apply readonly;

public class Readonly {

    private int capacity = 2; // readonly
    private String? msg = "readonly"; // readonly

    private int uninitializedCapacity; // error (uninitialized readonly)
    private String uninitializedMsg;    // error (uninitialized readonly)

    private mutable String? mutableMsg = "mutable";  // mutable

    public static void printMessages(String? mutableParam, String? readonlyParam) {

        mutableParam = "try to change"; // error
        readonlyParam = "try to change"; // error

        System.out.println("mutableParam: " + mutableParam);
        System.out.println("readonlyParam: " + readonlyParam);
    }

    public static void main(String[] args) {
        var readonly = new Readonly();
        String? mutableMsg = "changed mutable";

        readonly.capacity = 10; // error
        readonly.msg = "new readonly"; // error

        readonly.mutableMsg = mutableMsg;

        printMessages(readonly.msg, mutableMsg);

        System.out.println("mutableMsg: " + readonly.mutableMsg);
        System.out.println("capacity: " + readonly.capacity);
        System.out.println("msg: " + readonly.msg);
    }
}

Generated Java Code

package jadex.example;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import jadex.runtime.SafeAccess;

//apply readonly;

@NullMarked
public class Readonly {

    private final int capacity = 2; // readonly
    private final @Nullable String msg = "readonly"; // readonly

    private final int uninitializedCapacity; // error (uninitilaized readonly)
    private final String uninitializedMsg; // error (uninitilaized readonly)

    private @Nullable String mutableMsg = "mutable";  // mutable

    public static void printMessages(final @Nullable String mutableParam, final @Nullable String readonlyParam) {

        mutableParam = "try to change"; //error
        readonlyParam = "try to change"; //error

        System.out.println("mutableParam: " + mutableParam);
        System.out.println("readonlyParam: " + readonlyParam);
    }

    public static void main(final String[] args) {
        final var readonly = new Readonly();
        final @Nullable String mutableMsg = "changed mutable";

        readonly.capacity = 10; //error
        readonly.msg = "new readonly"; //error

        readonly.mutableMsg = mutableMsg;

        printMessages(readonly.msg, mutableMsg);

        System.out.println("mutableMsg: " + readonly.mutableMsg);
        System.out.println("capacity: " + readonly.capacity);
        System.out.println("msg: " + readonly.msg);
    }
}

New Additions

JSpecify @NullMarked Annotation Support


This feature is available starting from JADEx v0.42. Since the IntelliJ Plugin for JADEx v0.42 has not yet been published on the JetBrains Marketplace, if you wish to try it, please download the JADEx IntelliJ Plugin from the link below and install it manually.

JADEx v0.42 IntelliJ Plugin

We highly welcome your feedback on JADEx.

Thank you.

#programming

threaded - newest