From users-return-3395-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Sat Mar 24 13:08:45 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 8A56418064F for ; Sat, 24 Mar 2018 13:08:44 +0100 (CET) Received: (qmail 42478 invoked by uid 500); 24 Mar 2018 12:08:38 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 42466 invoked by uid 99); 24 Mar 2018 12:08:38 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 24 Mar 2018 12:08:38 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 2653FC1C2B for ; Sat, 24 Mar 2018 12:08:38 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.98 X-Spam-Level: * X-Spam-Status: No, score=1.98 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=2, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled Authentication-Results: spamd1-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=asert-com-au.20150623.gappssmtp.com Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id vIuJFJQerYLO for ; Sat, 24 Mar 2018 12:08:36 +0000 (UTC) Received: from mail-oi0-f41.google.com (mail-oi0-f41.google.com [209.85.218.41]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id ABC7E5F190 for ; Sat, 24 Mar 2018 12:08:35 +0000 (UTC) Received: by mail-oi0-f41.google.com with SMTP id c3-v6so12468577oib.5 for ; Sat, 24 Mar 2018 05:08:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=asert-com-au.20150623.gappssmtp.com; s=20150623; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to; bh=UyszXmg84Wv//EjPmijFTJqrBsgK20MDtAFEzi9dsPA=; b=tpOBlH5+iQEfPFKhxtv2MdIhtqPSpIPP8ZD8z1N5En4y4n4UyAIhS6NaVjy5SbJw+0 FtIVBjng9SCDOL0rsnMvHCVpvdh6eHMTWpSaCydMPWaqSbqLZUDoPLMEYmzcQ/KEOCu6 BG7g6GK3v6Es6JN+4gIFspR9TmfsQNfxajyinPB9trDaRlc3AnHCeq3/NGeOwOc6NIat huDngaIUwNjZJMxC7P3Of69rpOZXMNyMxi93UglYKa4aTBRCUZ8FVpdFc4HWnnwj8aJU 5JrsJ4w1eF1WNQC84biMsQyAVUNIT27cMkab6tNkb6Kvqdl6pzwEuvQDVOSf1ACVX7LQ xfSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:reply-to:in-reply-to:references :from:date:message-id:subject:to; bh=UyszXmg84Wv//EjPmijFTJqrBsgK20MDtAFEzi9dsPA=; b=jd9iKn9EFbJHNHb5CJfwrbmpE2EWIOC2peMWJil6k48mVdb9LwQzy28nRBNw93VDGU PwFe5upWm+DInbJUxUfL1Hv51948rbCxusm28mCSq8rEWNaQuHiL8/64IPklXHZ9mSB8 Uy9JMxLU+4vNvfn+TnughNTsxU4gxjjhpsd19NdzU+7Yjp7W3QRsaAOrfCrrV+3LNJnK lhEMu5/rrhYVPwd852VV0LSH5yNKAiTgYm5OYVFd6+4Q5pJV+FQYNCsXyuS/E+TwqwSZ mJ622EuWQUlPgZPSOvOhrZbPftXkHJA8195t8cxDZ7Sjrz03/xcdd+l/DOOagToGzx2z +CSQ== X-Gm-Message-State: AElRT7HQLAbnvjEJZhTENyuLBckRxfVQRAPP9S9Kytu3EkX1HApar3wU ChuGoMsleywL4L7xEedWkfXsWN/xBkUhgBsWWSsC0Ny+ X-Google-Smtp-Source: AG47ELsouRGEkPhmGIT5geL3WE0+hWMIm/3lhCJ1cQvjHurSi5EH5IMufUU/Z1+CorBPwwMySai5ZDeIPjOdl+VglOY= X-Received: by 10.202.58.196 with SMTP id h187mr8233443oia.208.1521893314477; Sat, 24 Mar 2018 05:08:34 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.22.148 with HTTP; Sat, 24 Mar 2018 05:08:33 -0700 (PDT) Reply-To: paulk@asert.com.au In-Reply-To: References: From: Paul King Date: Sat, 24 Mar 2018 22:08:33 +1000 Message-ID: Subject: Re: JDK 10: Use of var over def? To: users@groovy.apache.org Content-Type: multipart/alternative; boundary="001a113cd3fa655c840568276634" --001a113cd3fa655c840568276634 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I don't see a var in your example? Basically, we can use def and have flow typing (which would be the behavior if we make var always an alias for def) or we can behave like Java: // Java import java.util.Date; public class Main { public static void main(String[] args) { var x =3D new Object() { public void myMethod() { System.out.println(new Date());} }; x.myMethod(); // okay because the inferred type is the AIC var y =3D new Main(); y =3D new Main() { // reassignment okay because new type is subclass of inferred type public void myMethod() { System.out.println(new Date());} }; // y.myMethod(); // <=3D=3D=3D error: cannot find symbol // symbol: method myMethod() // location: variable y of type Main // y =3D new Integer(3); // <=3D=3D=3D error: incompatible types: Integer c= annot be converted to Main } } which effectively means that we infer as we do now but no flow typing. Conceptually, you may think that in the above Java example that var y has type Main (typeof RHS) but that isn't reflected in the bytecode in the same way that a type for a field or parameter would be reflected, so it's much more a concept that the compiler knows about rather than a concept that the bytecode knows about. Currently flow typing allows both the above "error" lines to succeed. But to behave like Java, we need both to fail. Cheers, Paul. On Sat, Mar 24, 2018 at 3:14 AM, MG wrote: > Hi Paul, > > wouldn't it make sense to combine flow typing with var x =3D RHS being > identical to typeof(RHS) x =3D RHS : > > @Canonicalstatic class Foo { > int x} > @InheritConstructorsstatic class SonOfFoo extends Foo { > int sonOfFooMethod() { 2*x } > } > > @Test@Ignore@CompileStaticvoid flowTypedVar() { > SonOfFoo f =3D new SonOfFoo(21) > //f =3D new Foo(-1) // compile time fails with "Groovyc: [Static type c= hecking] - Cannot assign value of type groovy.GroovyGeneralSpike$Foo to var= iable of type groovy.GroovyGeneralSpike$SonOfFoo" //f.sonOfFooMethod() /= / compile time fails with "Groovyc: [Static type checking] - Cannot find ma= tching method groovy.GroovyGeneralSpike$Foo#sonOfFooMethod()." if(f instan= ceof SonOfFoo) { > println f.sonOfFooMethod() // works because of flow typing } > } > > Cheers, > mg > > > > > On 23.03.2018 15:42, Paul King wrote: > > The Parrot parser already has support for this at the grammar level but w= e > regard some of the current implementation details as experimental. > > At the moment it is almost just an alias for "def" but discussions have > been around whether we can make the behavior closer to Java when used > within static Groovy code. We haven't defined exactly what this would mea= n > yet but roughly I suspect it could mean the same inferencing as now but > without flow typing. > > Cheers, Paul. > > > On Fri, Mar 23, 2018 at 10:12 PM, Merlin Beedell > wrote: > >> I see that the newly release JDK 10 now supports the =E2=80=9Cvar=E2=80= =9D declaration >> for local variables where the type can clearly be inferred from its >> initialiser: >> >> >> >> http://openjdk.java.net/jeps/286 >> >> >> >> I note that Groovy=E2=80=99s =E2=80=9Cdef=E2=80=9D syntax (among others)= was mentioned but >> rejected. >> >> >> >> Would Groovy move to support this syntax in the same way (support =E2=80= =98var=E2=80=99 >> only for Type Safe inferred declarations) or as a general alias to the >> =E2=80=9Cdef=E2=80=9D keyword? >> >> >> >> JDK 10 also has support for =E2=80=9Cdocker=E2=80=9D containers. The ec= osystem has >> certainly shifted! >> >> >> >> Merlin Beedell >> >> >> > > > --001a113cd3fa655c840568276634 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I don't see a var in your example?

= Basically, we can use def and have flow typing (which would be the behavior= if we make var always an alias for def) or we can behave like Java:
// Java
import java.util.Date;

= public class Main {
publi= c static void main(String[] args) {
var x =3D new Object() {
public void myMethod() { System.out.println(new Date());}
};
x.myMethod(); // okay because the inferred type = is the AIC
var y =3D new= Main();
y =3D new Main(= ) { // reassignment okay because new type is subclass of inferred type
public void myMethod() { Sy= stem.out.println(new Date());}
= };

// y.myMethod(); // <=3D=3D=3D error: cannot find symbol
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 // symbol: method myMethod()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // locatio= n: variable y of type Main

// y =3D new Integer(3); // <=3D=3D=3D error: incompati= ble types: Integer cannot be converted to Main
}
}

<= div>which effectively means that we infer as we do now but no flow typing. = Conceptually, you may think that in the above Java example that var y has t= ype Main (typeof RHS) but that isn't reflected in the bytecode in the s= ame way that a type for a field or parameter would be reflected, so it'= s much more a concept that the compiler knows about rather than a concept t= hat the bytecode knows about. Currently flow typing allows both the above &= quot;error" lines to succeed. But to behave like Java, we need both to= fail.

Cheers, Paul.


On Sat, Mar 24, 2018= at 3:14 AM, MG <mgbiz@arscreat.com> wrote:
=20 =20 =20
Hi Paul,

wouldn't it make sense to combine flow typing with=C2=A0 var x =3D = RHS=C2=A0 being identical to=C2=A0 typeof(RHS) x =3D RHS=C2=A0 :

@Canonical
static class F=
oo {
  int x
}

@InheritConstructors
static class S=
onOfFoo extends Foo {
  int sonOfFooMethod(=
) { 2*x }
}


@Test
@Ignore
@CompileStatic
void flowTyped=
Var() {
  SonOfFoo f =3D new =
SonOfFoo(21)
  //f =3D new Foo(-1) // co=
mpile time fails with "Groovyc: [Static type checking] - Cannot assign=
 value of type groovy.GroovyGeneralSpike$Foo to variable of type groovy.Gro=
ovyGeneralSpike$SonOfFoo"
  //f.sonOfFooMethod=
()   // compile time fails with "Groovyc: [Static type checking] - Can=
not find matching method groovy.GroovyGeneralSpike$Foo#sonOfFooMethod(=
)."
  if(f instanceof SonOfFoo) {
    println f.sonOfFooMethod()  // works because of flow typing
  }
}
Cheers,
mg




On 23.03.2018 15:42,= Paul King wrote:
The Parrot parser already has support for this at the grammar level but we regard some of the current implementation details as experimental.

At the moment it is almost just an alias for "def"= but discussions have been around whether we can make the behavior closer to Java when used within static Groovy code. We haven't defined exactly what this would mean yet but roughly I suspect it could mean the same inferencing as now but without flow typing.

Cheers, Paul.


On Fri, Mar 23, 2018 at 10:12 PM, Merlin Beedell <MBeedell@cryoserver.com> wrote:

I see that the newly release J= DK 10 now supports the =E2=80=9Cvar=E2=80=9D declaration f= or local variables where the type can clearly be inferred from its initialiser:

=C2=A0

http://openjdk.java.net/jeps/286<= /span>

=C2=A0

I note that Groovy=E2=80=99s = =E2=80=9Cdef=E2=80=9D syntax (among others) was mentioned but rejected.

=C2=A0

Would Groovy move to support t= his syntax in the same way (support =E2=80=98var=E2=80=99 o= nly for Type Safe inferred declarations) or as a general alias to the =E2=80=9Cdef=E2=80=9D keyword?

=C2=A0

JDK 10 also has support for =E2=80=9Cdocker=E2=80=9D containers.=C2=A0 The ecosyste= m has certainly shifted!

=C2=A0

Merlin Beedell

=C2=A0




--001a113cd3fa655c840568276634--