Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!.POSTED!not-for-mail From: Sebastian Newsgroups: comp.lang.java.programmer Subject: Re: are lambdas a way to bring functional programming to java? Date: Tue, 01 Jul 2014 21:52:02 +0200 Organization: albasani.net Lines: 64 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net 67ZFlY2KicXDbziRoTcSvI3EX20i3UfFsSUNCPBSrPOGxmPdMrF5+KWQOHDPK4BFRx2IvgPTcG9gH0zUb2XE5g== NNTP-Posting-Date: Tue, 1 Jul 2014 19:52:03 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="/CgYhHCOry4RxWN4pFaxSKbylzawVZYMe8Dv7kFLq6gvy7sHakPX1lHHs38cLvIdeEkWiHVKjeErtmY2fNZ9Q3pWYW+QrqM+EgUNVex7tIqEAV4UA95SD5X7RDOfVml4"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: Cancel-Lock: sha1:+9mRSaC23Pjv5BUDm4pRUofENck= Xref: csiph.com comp.lang.java.programmer:28890 Am 01.07.2014 20:46, schrieb Wanja Gayk: > In article , Wanja Gayk > (brixomatic@yahoo.com) says... > > [..] >>> Would it be a reasonable thing to say that a lambda is just a terser >>> way to implement an instance of a one-use anonymous class that has a >>> single method? >> >> Not quite, even though it looks a lot like it was. > [..] >> http://medianetwork.oracle.com/video/player/2623576348001 >> I found it pretty interesting. > > Sorry, that rather focussed on today's Lambda performance. > This one gives a better overview over the differences to anonymous > classes: > http://medianetwork.oracle.com/video/player/1785479333001 > > Kind regards, > Wanja > Thanks for the pointer. It appears one way that lambdas differ from inner classes is that you are actively discouraged from thinking of lambdas as instances of classes (objects). In particular, lambdas seem to have no well-defined identity, which is to say among other things one cannot meaningfully compare them with equals. Here‘s a source for this claim: Stuart Marks answer at http://stackoverflow.com/questions/15221659/java-8-lambda-expression-and-first-class-values from which I quote: Given that lambdas are converted into objects, they inherit (literally) all the characteristics of objects. In particular, objects: - have various methods like equals, getClass, hashCode, notify, toString, and wait - have an identity hash code - can be locked by a synchronized block - can be compared using the == and != and instanceof operators and so forth. In fact, all of these are irrelevant to the intended usage of lambdas. Their behavior is essentially undefined. You can write a program that uses any of these, and you will get some result, but the result may differ from release to release (or even run to run!). You might also want to see the page „Are lambda expressions objects?“ from the Lambda FAQ at http://www.lambdafaq.org/are-lambda-expressions-objects/ As Brian Goetz has memorably put it (on lambda-dev): "Lambdas-are-functions opens doors. Lambdas-are-objects closes them." So, as far as the intention behind lambdas goes, and also with respect to semantics and some technical differences (byte-code, optimization potential), it is not reasonable to say that "a lambda is just a terser way to implement an instance of a one-use anonymous class" - except as a first approximation that will serve you well in many circumstances. -- Sebastian