4 ms·
> That's it: a tiny adaptor that makes a bare function satisfy an interface. How is that possible? No, why is it necessary? In Java 8: public interface Ha
by twic 13y ago
> That's it: a tiny adaptor that makes a bare function satisfy an interface. How is that possible?
No, why is it necessary?
In Java 8:
public interface Handler {
void serveHTTP(ResponseWriter w, Request r);
}
public class MyApplication {
public static void myHandler(ResponseWriter w, Request r) {
w.write("An even easier webserver?");
}
public static void main(String... args) {
Http.handle("/", MyApplication::myHandler);
}
}
No adaptor needed!
Or even:
public class MyApplication {
public static void main(String... args) {
Http.handle("/", (w, r) -> w.write("An even easier webserver?"));
}
}
And please bear in mind that this is Java, the slowest-moving and least sophisticated of contemporary languages! Why does Go require so much more boilerplate than Java?
- dvirsky 13y agoIt's just a design decision of the http library, that its router by default accepts an interface and not a function - so internally you need this little adapter to make the function comply with that interface. I also loved this little trick when I noticed it. But if the HTTP library would directly accept a function, you could then just pass a function without that trick. Go doesn't mandate you use interfaces for callbacks (like Java did until 1.8), you can also define an API that accepts functions directly, provided they have a certain signature. Also, the standard library hides this little trick from you, and just exposes the HandleFunc method, that lets you register plain functions as handlers. as in this example: http://golang.org/pkg/net/http/#ListenAndServe http://golang.org/pkg/net/http/#ListenAndServe
- tomp 13y agoIf I understand the Java 8 example correctly, the Http.handle accepts an interface as well (the Handler interface with a single method). Java compiler is smart enough to automatically convert functions into objects satisfying interfaces with exactly one method, while Go compiler isn't.
- dvirsky 13y agoYeah, well, knowing the Go philosophy, it makes sense not to have the compiler solve ambiguities like that, especially since a function can have its own methods.
- colin_mccabe 13y agoUsually you don't want the compiler to be "smart" in this way, because it creates other headaches. For example, is a temporary object created each time you do this? What impact will that have on GC? If I compare an object of this interface with a "fake interface object" generated by the compiler, will it always be the same for the same callback? What is the call stack going to look like when I use jps? Seeing another <<anonymous>> line in there wouldn't give me the warm fuzzies when debugging. I was not a fan of auto-unboxing for ints, for the same reasons. There are a lot of good features in Java 8, but I'm not a fan of this particular one. Even if it saves a line of typing.
- dvirsky 13y agoBTW I don't think the compiler is "smart enough" - but the Http API has 2 (probably more) overloaded methods for each type you may want to pass to it. Go doesn't have method overloading by design. If that's something that is a must have for anyone, Go is simply not for them.
- twic 13y agoNo, it's exactly as tomp says - in Java 8, lambda expressions are handled by compiling them to little classes which implement interfaces. Kind of. The truth is that something absolutely barking mad goes on in the compiler, and then extra magic is injected at runtime: http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html http://cr.openjdk.java.net/~briangoetz/lambda/lambda-transla... But it behaves exactly as if there were little classes which implement interfaces. Just more efficient.
- dvirsky 13y agoOh, thanks for the info, didn't know that. Still I don't think that Go should have that feature.
- NateDad 13y agoWhat's nice about the go implementation is that it can trivially accept a function or a type with the right function.
- nnutter 13y agoDo you get to use Java 8 in production? Edit: To clarify I mean if you had a Java X app deployed how hard is it to upgrade to Java 8? One thing I like, maybe naively, about Go is static binaries.
- lmm 13y agoIt's pretty trivial. 1. Upgrade JVM on server (i.e. change 1 line of puppet config) 2. Build with Java 8 (i.e. change target option in your build file) 3. Deploy. I mean sure, you have to install the new JVM, but if you don't have a system in place for making changes to your servers then you've got bigger problems than which language you're using. And honestly I think the JVM with its classpath approach solves the library problem better than most platforms; upgrading the JVM binary occasionally is no great hardship, and aside from that everything is just jars, dynamic but not getting in the way of each other unless you want them to. What happens when security holes are found in Go libraries, do you have to recompile anything that depends on them?
- twic 13y agoPersonally, no, not yet. My current employer has been dragging its feet over Java upgrades, so we're only just transitioning onto Java 7. However, most of the work involved in doing that is paving the road so that version upgrades are faster. I would anticipate that moving from 7 to 8 will be much faster than moving from 6 to 7.