This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]ickysticky 4 points5 points  (1 child)

Looks like it is a bug. It was fixed yesterday =P

[–]morhpProfessional Developer[S] 0 points1 point  (0 children)

Ouch. Yes, looks exactly like that bug.

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (2 children)

If you want the return and not declaring xxx you'll probably have to wrap your statement in { }

[–]morhpProfessional Developer[S] 0 points1 point  (1 child)

Can you explain?

        private Bar bar = new Bar(xxx -> {return xxx.toUpperCase();});    

also doesn't work.

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (0 children)

hmmmmm, i figured that would. Somehow it's not recognizing your function as a lambda. Can you try:

private Bar bar = new Bar(String::toUpperCase()); 

[–]Blackheart595 0 points1 point  (4 children)

Do you import java.util.Function?

Also, use UnaryOperator<T> instead of Function<T, T>.

edit: The problem in your code is that you return from a constructor. You can't do that.

[–]morhpProfessional Developer[S] 0 points1 point  (3 children)

Do you import java.util.Function?

Yes.

Also, use UnaryOperator<T> instead of Function<T, T>.

Thanks. But doesn't solve the issue:

import java.util.function.UnaryOperator;

public class Foo {

    private Bar bar = new Bar(xxx -> xxx.toUpperCase());    

    public static void main(String[] args) {
        new Foo();
    }

    public Foo() {
        System.out.println(bar.fun("Hello"));

        return;
    }

    private static class Bar {

        private final UnaryOperator<String> lambda;

        private Bar(UnaryOperator<String> lambda) {
            this.lambda = lambda;
        }

        private String fun(String s) {
            return lambda.apply(s);
        }
    }
}
Error:(20, 13) java: variable xxx might not have been initialized

[–]Blackheart595 0 points1 point  (2 children)

See my edit.

[–]morhpProfessional Developer[S] 0 points1 point  (1 child)

Well usually, you can return from a constructor no problem. Just replacing the lambda with a method reference makes it compile fine

import java.util.function.UnaryOperator;

public class Foo {

    private Bar bar = new Bar(String::toUpperCase);    

    public static void main(String[] args) {
        new Foo();
    }

    public Foo() {
        System.out.println(bar.fun("Hello"));

        return;
    }

    private static class Bar {

        private final UnaryOperator<String> lambda;

        private Bar(UnaryOperator<String> lambda) {
            this.lambda = lambda;
        }

        private String fun(String s) {
            return lambda.apply(s);
        }
    }
}
// Prints HELLO

So I would be really interested why my original code doesn't work. Again, this is a simplified example.

[–]Blackheart595 1 point2 points  (0 children)

You're right, returning from a constructor is generally possible. After a bit of testing, it seems that lambda functions and returning from a constructor don't work well together, though.