0

I am wondering if there is an option to return a generic type from a Java 12 switch expression.

The basic code can look like that:

boolean result = switch(ternaryBool) {
    case TRUE -> true;
    case FALSE -> false;
    default -> throw new IllegalArgumentException("Seriously?!");
};

Is there anything against doing it this way?

T result = switch(ternaryBool) {
    case TRUE -> BooleanUtils.toBoolean('true');
    case FALSE -> new Integer(0);
    default -> throw new IllegalArgumentException("Seriously?!");
};

EDIT:

Maybe better example from my case: I need to have few classes which are representing primitive & complex data structures. I have also factory method which is creating this DataPointValue based on an enum from other system and unknown value (let's forget about casting exceptions):

public static <T> IDataPointValue create(T value, DATA_TYPE dataType) throws Exception {
    try {
        switch (dataType) {
            case BOOL:
                return new BoolDataPointValue((Boolean) value);
            case INT:
                return new IntDataPointValue((Integer) value);
            case WORD:
                return new IntDataPointValue((Integer) value);
            case STRING:
                return new StringDataPointValue((String) value);
            case REAL:
            case FLOAT:
                return new RealDataPointValue((Float) value);
            case DINT:
                return new DIntDataPointValue((Integer) value);
            case DWORD:
                return new DWordDataPointValue((Integer) value);
            default:
                throw new Exception("Data Type not implemented: " + dataType);
        }
    } catch (ClassCastException e){
        throw new Exception("Could not create DPV in terms of Type incompability");
    }
}

Is there any profit to move this code and use switch expression from Java 12?

user1803551
  • 12,965
  • 5
  • 47
  • 74

2 Answers2

1

Is there anything against doing it this way?

JEP 325: Switch Expressions (Preview) in one of the columns states the following about the extended representation of the switch statement(formatting mine):

A switch expression is a poly expression; if the target type is known, this type is pushed down into each arm.

The type of a switch expression is its target type if known; if not, a standalone type is computed by combining the types of each case arm.

Holding the above as true, the type of the switch expression as you wrote it, could possibly be an Object unless you've specifically formed a custom model and encapsulated the types.

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353
1

Is there any profit to move this code and use switch expression from java12?

This really depends on what you count as profit. There are a couple of things that immediately come to mind when looking at the corresponding switch expression:

public static <T> IDataPointValue create(T value, DATA_TYPE dataType) {
    return switch (dataType) {
        case BOOL ->        new BoolDataPointValue((Boolean) value);
        case INT ->         new IntDataPointValue((Integer) value);
        case REAL, FLOAT -> new RealDataPointValue((Float) value);
        // ...
    };
}
  1. You don't need a default case since the compiler knows you are dealing with all of the enum values (run-time changes to the enum will cause problems).
  2. It's a bit more readable since you return on the whole expression and not in each case explicitly, and since you don't need to deal with fallthrough semantics.

As for the generics side of the question, it's not that clear. For one, your method is declaring a genetic parameter without bounds and using it only once, so there is no real point in it. The declaration

public static <T> IDataPointValue create(T value, DATA_TYPE dataType)

is the same as

public static IDataPointValue create(Object value, DATA_TYPE dataType)

because T doesn't add any info. You might want to ask a new question about your data structures.

user1803551
  • 12,965
  • 5
  • 47
  • 74