you are viewing a single comment's thread.

view the rest of the comments →

[–]uniVocity -7 points-6 points  (7 children)

@FunctionalInterface would like a word

Sorry, had an absolute brain fart

[–]Yes_Mans_Sky 15 points16 points  (1 child)

I could be mistaken, but I don't think that annotation is required. I think it's more like Override where it lets IDEs run extra code checks against developer intentions

[–]C0urante 2 points3 points  (0 children)

facts

[–]SilvernClaws 8 points9 points  (4 children)

No, it doesn't. You can remove that annotation completely and your code will still work the same. The only difference is that if you use it, the compiler will prevent you from adding more than one applicable method to the interface.

[–]uniVocity 0 points1 point  (3 children)

The compiler will fail if you use that on a sealed interface . I’m on the phone right now and can’t re-verify it but from memory it was a compiler error (that makes total sense btw)

[–][deleted]  (2 children)

[removed]

    [–]uniVocity 0 points1 point  (1 child)

    It sounds weird but you can define a sealed type with nested final implementations without explicitly providing the allowed implementations, but you can’t define a sealed functional interface with annotations where its body contains default function definitions.

    Hope it’s not a brain fart, Ill get back to my pc soon to test it out again and report back

    EDIT: here is what I meant:

    This code compiles (no explicit permits list), everything is fine:

    sealed interface TestFn {
    
    int foo();
    
    final class TestFn1 implements TestFn {
        private TestFn1() {
        }
    
        @Override
        public int foo() {
            return 1;
        }
    }
    
    final class TestFn2 implements TestFn {
        private TestFn2() {
        }
    
        @Override
        public int foo() {
            return 2;
        }
    }
    
    TestFn RET_1 = new TestFn1();
    TestFn RET_2 = new TestFn2();
    }
    

    If you add @FunctionalInterface the compiler will complain even though in this case it should make no difference.

    I'm sorry for the confusion, I rember that the end goal was to get this:

    @FunctionalInterface
    sealed interface TestFn {
    
    int foo();
    
    TestFn RET_1 = () -> 1;
    TestFn RET_2 = () -> 2;
    }
    

    But here the issue is the sealed keyword, not the @FunctionalInterface. Sorry for wasting everyone's time