manifold-systems / manifold

Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.
http://manifold.systems/
Apache License 2.0
2.42k stars 125 forks source link

Add support for Field Access Expressions with operator overloading (prefixBind) #218

Open Argannor opened 4 years ago

Argannor commented 4 years ago

Feature Request In order to allow for operator overloading (specifically prefixBind) to be used in building DSLs it would be nice to support Field Access Expressions. This would also make prefixBind more intuitive for developers, as we're used to field access having precedence over any operators.

Therefore I'd propose supporting the following two cases:

package com.acme.mf;

public class FieldAccessExpressionTest {

    public static void main(String[] args) {
        caseA();
        caseB();
    }

    public static void caseA() {
        Foo foo = new Foo();
        Bean arg = new Bean("hello A");
        // Error: java: ';' expected
        String result = foo arg.someString;
        System.out.println(result);
    }

    public static void caseB() {
        Foo foo = new Foo();
        Bean arg = new Bean("hello B");
        // Error: java: ';' expected
        String result = foo arg.getSomeString();
        System.out.println(result);
    }

    public static class Foo {

        public String prefixBind(String str) {
            return str;
        }

    }

    public static class Bean {
        public String someString;

        public Bean(String someString) {
            this.someString = someString;
        }

        public String getSomeString() {
            return someString;
        }
    }

    public static class Bar {

        public Foo postfixBind(String str) {
            return new Foo();
        }

    }

}

Edit 05.10.2020: Rewritten from a bug report to a feature request.

rsmckinney commented 4 years ago

@Argannor Unit expression operands are limited to identifiers, literals, and parenthesized expressions. With test G since the expression on the left is an identifier and the one on the right is parenthesized, there is otherwise an ambiguity wrt a method call expression, thus that particular use-case is not supported. Perhaps this will be supported with a more advanced expression parser in a future release.

Argannor commented 4 years ago

@rsmckinney thank you for the quick reply. Sounds reasonable, in this case this is more of a feature request than a bug report.

As outlined above i don't think test G should be supported, B and C however would make operator overloading valuable for creating DSLs which can be neatly integrated in one's Java code. Therefore I'd be happy to see this in the future.

Do you want me to rephrase the original issue as a feature request?

rsmckinney commented 4 years ago

Do you want me to rephrase the original issue as a feature request?

Go for it. Thanks.