Am Samstag, 8. Oktober 2016 17:31:27 UTC+2 schrieb Pietro Gagliardi
(andlabs):
>
> In Java, if an interface contains exactly one method, and that method is
> not already part of java.lang.Object, the syntax
>
> Interface i = (arguments) -> {
> code
> };
>
> will make an object i of that interface type with the given closure as the
> method body. This interface is called a functional interface.
>
> Pre-Java 8, the same thing could be done with
>
> Interface i = new Interface() {
> public void method() {
> code
> }
> };
>
>
The number of methods in a Java interface is of course arbitrary and not
restricted to a single one. The first code sample above shows a Java8 lamda
expression which inevitably consists of the lambda function itself. Lambdas
in Java8 implement a variety of interfaces, but this does not mean that
interfaces in Java must only contain one method.
The biggest difference is that a "class" or struct in Go implicitly
implements an interface once it contains all the methods defined in some Go
interface. In Java a a class must explicitly implement an interface.
As what lambdas or closures are concerned Go supports full-fledges
closures, which means support for non-local returns, variables outside the
closure are writeable. This is not the case in Java and in Java you cannot
throw checked exceptions from within a closure. These limitations in Java
are all due to (binary) backwards compatibility and limitations of the JVM
(e.g. non-local returns).
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.