IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Thanks all
I know this is Java baby-talk to most of you, so your patience is appreciated.


Peter
[link|http://www.ubuntulinux.org|Ubuntu Linux]
[link|http://www.kuro5hin.org|There is no K5 Cabal]
[link|http://guildenstern.dyndns.org|Home]
Use P2P for legitimate purposes!
New Re: Thanks all
Well, I was kinda intimidated,

But now we can make the following statement

Outer Class = Regular Class
Inner Class = Class defined in another class

Inner classes are used to implement: 1-Callbacks, 2-iterators, 3-closures, 4-anonymous classes, 5-Factory

So now we have 5 more terminology to hunt, I would say Callbacks (the term I kow the least about) is probably the most interesting and hardest to learn

But now we have a new question ... does this mean we can not implement (or implement better), callbacks, iterators, closures, factories without inner classes!
New You misunderstand, on at least one issue:
"Systems" tries to sum up:
Inner classes are used to implement: 1-Callbacks, 2-iterators, 3-closures, 4-anonymous classes, 5-Factory
Nope, not #5 -- it's the other way around; the "Factory pattern" is often used to implement inner classes.


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
New No, they can be used as factories as well.
Spring uses this technique internally to its JDBC classes to provide PreparedStatement factories.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New s/callbacks/delegates/g
Or at least that's what they call them in C#. Java inner classes are a kludge deluxe, but I suppose they are useful in the right circumstances.
New Yep, one of the uglier useful things in Java. :-P
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New ICLRPD (new thread)
Created as new thread #194959 titled [link|/forums/render/content/show?contentid=194959|ICLRPD]
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Great - another perfectly good term hijacked and hosed (new thread)
Created as new thread #194973 titled [link|/forums/render/content/show?contentid=194973|Great - another perfectly good term hijacked and hosed]



"Whenever you find you are on the side of the majority, it is time to pause and reflect"   --Mark Twain

"The significant problems we face cannot be solved at the same level of thinking we were at when we created them."   --Albert Einstein

"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses."   --George W. Bush
New You can't do #3
Inner classes are the closest thing that Java has to closures.

But they aren't closures.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New For most uses...
The difference is vanishingly small.

The main difference is that a closure is a function (more or less) and an anonymous inner class is a class. Another difference is that anonymous classes can't change method local variables, but this can be worked around. So while the syntax is definitely not nearly as nice, the functionality is pretty much there.

You might find this interesting as well: [link|http://c2.com/cgi/wiki?BlocksInJava|http://c2.com/cgi/wiki?BlocksInJava]
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New I did find that interesting. One major complaint down.
The fact that it is ugly isn't a new complaint. Java generally is ugly, I expect that.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New You'll find this interesting too - Bistro
[link|http://bistro.sourceforge.net/overview.htm|http://bistro.source....net/overview.htm]

Its a preprocessor that provides smalltalk-ish syntax and capabilities on top of Java. It generates inner classes for the closures. Its been around for at least 5 years. More of a proof of concept than a practical language - there's no debugger support or anything. But the paper is interesting.

Also, FWIW, Squeak doesn't have proper closures out of the box - it has BlockContext's instead which also don't do a good job of hanging onto local variables properly (there is a workaround you can use but you have to send the thing a message fixTemps to ask it to hang onto the temp vars).

There is a closure implementation done for Squeak, its waiting to be adopted. I think it will end up in there eventually.



"Whenever you find you are on the side of the majority, it is time to pause and reflect"   --Mark Twain

"The significant problems we face cannot be solved at the same level of thinking we were at when we created them."   --Albert Einstein

"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses."   --George W. Bush
New They sorta stink as closures
Not what Ben or Todd would call a "closure" at all.
--


- I was involuntarily self-promoted into management.

[link|http://kerneltrap.org/node/4484|Richard Stallman]

New You need Jim Weirich's...
...handy dandy [link|http://www.clug.org//ml/archive/programming/1998-11/msg00014.html|Y Combinator in Java].

(Me suspects that's why he switched to Ruby). :-)
New Without being rude...
...what the devil IS that thing?


Peter
[link|http://www.ubuntulinux.org|Ubuntu Linux]
[link|http://www.kuro5hin.org|There is no K5 Cabal]
[link|http://guildenstern.dyndns.org|Home]
Use P2P for legitimate purposes!
New A recursive anonymous function...
...that takes a function as an argument and returns the function applied to the original function. Or in lambda terminology:

   λx.x(λx)

[edit: The Y combinator is a good test of how well closures are implemented].
Expand Edited by ChrisR Feb. 16, 2005, 03:23:52 PM EST
New Is it just interesting, or is it useful?
I would imagine such a thing would have to be carefully used if one is to avoid unpleasant runaway circumstances.


Peter
[link|http://www.ubuntulinux.org|Ubuntu Linux]
[link|http://www.kuro5hin.org|There is no K5 Cabal]
[link|http://guildenstern.dyndns.org|Home]
Use P2P for legitimate purposes!
New Well, in Java it's pretty useless.
New It's a pretty basic feature found in functional languages
like haskell. How useful it would be in the context of a language like java, I don't know. You might make it part of an interpreter for a functional language implemented in java...
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Re: You need Jim Weirich's...
> ...handy dandy Y Combinator in Java [*].

Egads, does that bring back memories.

> (Me suspects that's why he switched to Ruby). :-)

Ummm ... My switch happened about 2 years latter, and not because of Y combinators in particular, but the economy of expression was certainly a driving factor (BTW: Y Combinator in Ruby: [link|http://www.rubygarden.org/ruby?TheScaryDoor|http://www.rubygarde...ruby?TheScaryDoor] ... slightly better, although the note that the title of the page is the Scary Door).
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New I've just followed the first step of execution
of this thing using the Lisp evaluation model. I can see it working. My head spins. I still don't understand how it works!
--


- I was involuntarily self-promoted into management.

[link|http://kerneltrap.org/node/4484|Richard Stallman]

New I understand it!
Better yet, I think that I can explain it.

This is all much simpler if we allow ourselves functions of 2 variables. It is fairly easy to build a recursive function of one variable starting with functions of 2 variables:
\nF = proc { |recurse, n|\n  if n == 0 then\n    1\n  else\n    n * recurse.call(recurse, n-1)\n  end\n}\n\nY = proc { |builder, n|\n  builder.call(builder, n)\n}\n\np Y.call(F, 5)  # prints 120\n

The idea being that Y calls F with F as an argument, and that sets up the recursion. You can even write that directly without the temporary variables Y and F just by expanding out Y and F in the expression Y.call(F,5):
\np proc {|builder, n|\n    builder.call(builder, n)\n  }.call(\n    proc { |recurse, n|\n      if n == 0 then\n        1\n      else\n        n * recurse.call(recurse, n-1)\n      end\n    },\n    5\n  )\n

But a brilliant guy named Haskell Curry has a clever trick, you don't need functions of 2 variables if you have functions that return functions. The translation is perfectly mechanical. The idea is that something like:
\nX = proc {|foo, bar|\n  ...\n}\nX.call(this, that)\n

can always be written as
\nX = proc {|foo| proc{ |bar|\n  ...\n}}\nX.call(this).call(that)\n

where ... remains unchanged. So edit the declaration and the calls to functions of 2 variables, and we only need functions of one variable.

Let's apply this mechanical translation to F above. We had:
\nF = proc { |recurse, n|\n  if n == 0 then\n    1\n  else\n    n * recurse.call(recurse, n-1)\n  end\n}\n

Since we now want both recurse and F to be functions of 1 variable, we get the following:
\nF = proc { |recurse| proc { |n|\n  if n == 0 then\n    1\n  else\n    n * recurse.call(recurse).call(n-1)\n  end\n}}\n

And likewise Y becomes:
\nY = proc { |builder| proc { |n|\n  builder.call(builder).call(n)\n}}\n

and our call becomes:
\np Y.call(F).call(5)\n

Or the complex version with no intermediate variables becomes:
\np proc {|builder| proc { |n|\n    builder.call(builder).call(n)\n  }}.call(\n    proc { |recurse| proc { |n|\n      if n == 0 then\n        1\n      else\n        n * recurse.call(recurse).call(n-1)\n      end\n    }}).call(\n    5\n  )\n

Which is, modulo formatting, exactly what Jim wrote.

That runs, but following the execution should make your head spin... :-)

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Read it out loud
Remember the old rhyme/mnemonic for spelling Mississippi when you were six?

"Em - eye - crooked letter - crooked letter - eye ...
crooked letter - crooked letter - eye ...
humpback - humpback - eye."

Now read this:

p proc {|builder| proc { |n|\n    builder.call(builder).call(n)
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New I detect glazed over eyes
I was going to cement that by writing a version of factorial that just used recursion (without assignment of course!) to reduce everything down to +1, -1. But Ruby lost track of how many braces were open. :-(

There's a moral there somewhere...

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New This was as brilliant an explanation as I've ever seen
Thank you.

(not that my head spins any less)
--


- I was involuntarily self-promoted into management.

[link|http://kerneltrap.org/node/4484|Richard Stallman]

     OT: Inner/outer classes. - (pwhysall) - (30)
         Simple explanation: - (admin)
         Wheras an "outer" class is just an ordinary class. -NT - (CRConrad)
         There's some other types as well - (ChrisR)
         Also how Java fakes friendship - (tuberculosis)
         Inner class is declared inside another class - (Arkadiy)
         Thanks all - (pwhysall) - (24)
             Re: Thanks all - (systems) - (23)
                 You misunderstand, on at least one issue: - (CRConrad) - (1)
                     No, they can be used as factories as well. - (admin)
                 s/callbacks/delegates/g - (ChrisR) - (3)
                     Yep, one of the uglier useful things in Java. :-P -NT - (admin)
                     ICLRPD (new thread) - (drewk)
                     Great - another perfectly good term hijacked and hosed (new thread) - (tuberculosis)
                 You can't do #3 - (ben_tilly) - (3)
                     For most uses... - (admin) - (2)
                         I did find that interesting. One major complaint down. - (ben_tilly) - (1)
                             You'll find this interesting too - Bistro - (tuberculosis)
                 They sorta stink as closures - (Arkadiy) - (12)
                     You need Jim Weirich's... - (ChrisR) - (11)
                         Without being rude... - (pwhysall) - (4)
                             A recursive anonymous function... - (ChrisR) - (3)
                                 Is it just interesting, or is it useful? - (pwhysall) - (2)
                                     Well, in Java it's pretty useless. -NT - (ChrisR)
                                     It's a pretty basic feature found in functional languages - (jake123)
                         Re: You need Jim Weirich's... - (JimWeirich) - (5)
                             I've just followed the first step of execution - (Arkadiy) - (4)
                                 I understand it! - (ben_tilly) - (3)
                                     Read it out loud - (drewk) - (1)
                                         I detect glazed over eyes - (ben_tilly)
                                     This was as brilliant an explanation as I've ever seen - (Arkadiy)

Powered by blind unix!
168 ms