From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,aef01dc1d0a3a8bd X-Google-Attributes: gid103376,public From: Hyman Rosen Subject: Re: Dummy Date: 2000/02/07 Message-ID: #1/1 X-Deja-AN: 582883095 Sender: hymie@calumny.jyacc.com References: <387b154a.3533365@newsread.albacom.net> <3898C380.BC01EC03@earthlink.net> <3899DB45.B481F8E2@averstar.com> <87bt5wlt8l.fsf@deneb.cygnus.argh.org> <873dr4pz44.fsf@deneb.cygnus.argh.org> X-Complaints-To: abuse@panix.com X-Trace: news.panix.com 949967247 1929 209.49.126.226 (7 Feb 2000 23:47:27 GMT) Organization: PANIX Public Access Internet and UNIX, NYC NNTP-Posting-Date: 7 Feb 2000 23:47:27 GMT Newsgroups: comp.lang.ada Date: 2000-02-07T23:47:27+00:00 List-Id: Florian Weimer writes: > This prints `3' and `4', doesn't it? Cool. I didn't know that Java > supports class closures. Now Java only needs operator overloading > and some syntactic sugar for dispatching by name and introspection, > and Java will become my favorite scripting language. ;) Actually, I'm wrong about this. I was over-ambitious. The following similar code does work, though. The trick is that the "closures" are fake - behind the scenes, Java copies the referenced locals into the local class, and only lets you do this with "final" variables. class a { interface i { int get(); void set(int n); } class my_int implements i { int val; public int get() { return val; } public void set(int n) { val = n; } } i local_ref() { final my_int local = new my_int(); return new i () { public int get() { return local.get(); } public void set(int n) { local.set(n); } }; } public static void main(String[] v) { a my_a = new a(); i i1 = my_a.local_ref(); i i2 = my_a.local_ref(); i1.set(3); i2.set(4); System.out.println(i1.get()); System.out.println(i2.get()); } }