Skip to content

generation of null-safe expressions involving Java Optional #256

@abstratt

Description

@abstratt

Should use Java Optional to implement null-safe object navigation.

Examples:

operation f1(a : Foo[0,1]) :Integer;
begin
    var x : String[0,1];
    x := self.attr1;
    return a?.m2(!!x, 3) ?: -1;
end;

Java code:

String x = this.getAttr1();
return Optional.ofNullable(a)
    .map(it -> 
        it.m2(Object.requireNotNull(x), 3)
    )
.orElseGet(
    () -> -1
);

Action breakdown:

/* AddVariableValue: x */ x :=
        /* ReadSelfAction */ self 
            /* ReadStructuralFeatureValue: attr1 */ .attr1
;

/* AddVariableValue: _result */ return
    /* ConditionalNode «DefaultValueExpression» -> Integer[1,1] */
        /* Clause 0 */
            /* Body */
                /* ReadVariable: a -> Foo[0,1] */ a
                    /* CallOperation: m1 -> Integer[0,1] */ ?.m2(
                        /* SAN «Cast Required» -> Integer[1,1] */
                            /* ReadVariable: x Integer[0,1] */,
                        /* LiteralValueSpecification: 3 -> Integer[1,1] */ 3
        /* Clause 1 */    
            /* Body */
                /* LiteralValueSpecification: -1 -> Integer[1,1] */ -1
        );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions