Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B767117792 for ; Tue, 22 Sep 2015 09:37:01 +0000 (UTC) Received: (qmail 72058 invoked by uid 500); 22 Sep 2015 09:36:56 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 72027 invoked by uid 500); 22 Sep 2015 09:36:56 -0000 Mailing-List: contact users-help@groovy.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.incubator.apache.org Delivered-To: mailing list users@groovy.incubator.apache.org Received: (qmail 72017 invoked by uid 99); 22 Sep 2015 09:36:56 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 Sep 2015 09:36:56 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id B8EBFC0CDC for ; Tue, 22 Sep 2015 09:36:55 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 3.879 X-Spam-Level: *** X-Spam-Status: No, score=3.879 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_REPLY=1, HTML_MESSAGE=3, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id wZB5v3ZpXZZq for ; Tue, 22 Sep 2015 09:36:54 +0000 (UTC) Received: from mail-wi0-f172.google.com (mail-wi0-f172.google.com [209.85.212.172]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with ESMTPS id 275A5209B7 for ; Tue, 22 Sep 2015 09:36:54 +0000 (UTC) Received: by wicfx3 with SMTP id fx3so182924393wic.1 for ; Tue, 22 Sep 2015 02:36:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=/C4w8foGpE/pVRoggGJRydKomFL40+tqFJ1zj0rKWZQ=; b=fne228rqHwLdvuF5GRdKKwS+dzPsVd/Ytj6vRvqdOhdrXp3KI0wp+Xca97+w1a25Lx lZL6TJbDzbNHQGYd1FwzDxpk0xgwq2vwgVqXImgJW6V0Kjm/frHwWyKj8e3pJrSyah/4 Vj0OOnptsyFummd2bGH+l/uUEnEOI47qWiZDCbuoz6fsSBPr48Hi1sCxFnc9wkAmksEi 5l2b3s56M8lx0M52ZDJuc5wTpyXgL6ODY2XgY+DsI6bA78hkG/HDyd8SNfDvxLwhuqz6 fhA7oUINYsJH3GU/jm60ri75KVkUR9b7KX9pdf8+YZo1RY8KtaH7fjq1w2GGbMt7oilM qjzQ== MIME-Version: 1.0 X-Received: by 10.180.37.232 with SMTP id b8mr19616485wik.46.1442914613824; Tue, 22 Sep 2015 02:36:53 -0700 (PDT) Received: by 10.194.17.38 with HTTP; Tue, 22 Sep 2015 02:36:53 -0700 (PDT) In-Reply-To: References: Date: Tue, 22 Sep 2015 10:36:53 +0100 Message-ID: Subject: Re: infer return type of script From: Jamie Echlin To: users@groovy.incubator.apache.org Content-Type: multipart/alternative; boundary=e89a8f647253ff942c052052bc9f --e89a8f647253ff942c052052bc9f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable No, I guess so. So it sounds like you're saying I should forget it and move on to the next thing? I.e. it's not feasible to get at this in a static analysis context? On Tue, Sep 22, 2015 at 10:24 AM, C=C3=A9dric Champeau wrote: > Just for context and history, early versions of the type checker had > return type inference for methods too. However, we removed it for a good > reason: overrides. A method "foo" in class A can have an inferred type T, > but "foo" from B extends A could very well have a different inferred type > T2. So you can make no guarantee, at least for non private methods. > > > 2015-09-22 11:19 GMT+02:00 Jamie Echlin : > >> Hi Shil, >> >> I did that actually, but it tells me the return type of the run method o= f >> Script is Object. Which is expected, if it's just looking at the actual >> method signature. >> >> But what I was hoping for is that given a script: >> "new Double(2)" >> >> it would be able to infer the actual return type is a Double. Now, I can >> imagine scripts where you randomly pick from any class on the classpath, >> invoke a no-args constructor, and return that... in that case I don't >> expect magic. But most scripts have one or two possible return statement= s >> (maybe implicit), I'm wondering if there is already code that can >> statically analyse a method/script and say that there are only one, two = or >> whatever possible return types. >> >> I'll take a look at ReturnAdder, thanks for that. >> >> cheers, jamie >> >> >> On Mon, Sep 21, 2015 at 11:55 PM, Shil Sinha >> wrote: >> >>> Actually, the second option can be made safe by initializing ReturnAdde= r >>> with a listener, which will allow you to 'visit' all implicit returns >>> without actually adding return statements. >>> >>> On Mon, Sep 21, 2015 at 5:30 PM, Shil Sinha >>> wrote: >>> >>>> You could call 'getInferredReturnType' on the 'run' method node of you= r >>>> script class, but inferred return types only appear to be stored for >>>> closures and certain binary expressions (based on the usages of the >>>> INFERRED_RETURN_TYPE marker and the storeInferredReturnType method). >>>> >>>> You could also add all implicit return statements to the 'run' method >>>> via ReturnAdder, and then infer a return type by visiting all of the r= eturn >>>> statements, but I'm not sure if that kind of AST modification in a typ= e >>>> checking extension is safe. >>>> >>>> Shil >>>> >>>> On Mon, Sep 21, 2015 at 4:34 PM, Jamie Echlin >>>> wrote: >>>> >>>>> Hi... >>>>> >>>>> Is it possible to infer the return type, or types of a script during >>>>> static compilation? Presumably there is always a finite list of branc= hes >>>>> where the script could end. >>>>> >>>>> I'm looking >>>>> at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor#getInf= erredReturnType, >>>>> but I'm not really sure how to call it from a type checking script, n= or >>>>> what adds the inferred return type to the metadata which is used in t= hat >>>>> method. >>>>> >>>>> cheers, jamie >>>>> >>>> >>>> >>> >> > --e89a8f647253ff942c052052bc9f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
No, I guess so. So it sounds like you're saying I should fo= rget it and move on to the next thing? I.e. it's not feasible to get at= this in a static analysis context?
<= br>
On Tue, Sep 22, 2015 at 10:24 AM, C=C3=A9dric= Champeau <cedric.champeau@gmail.com> wrote:
Just for context and history, e= arly versions of the type checker had return type inference for methods too= . However, we removed it for a good reason: overrides. A method "foo&q= uot; in class A can have an inferred type T, but "foo" from B ext= ends A could very well have a different inferred type T2. So you can make n= o guarantee, at least for non private methods.


2015-09-22 11:19 GMT+02:00 Jamie Echlin = <jamie.echli= n@gmail.com>:
H= i Shil,

I did that actually, but it tells me the return type of the ru= n method of Script is Object. Which is expected, if it's just looking a= t the actual method signature.

But what I was hoping for is that given= a script:
"new Double(2)"

it would be able to infer the act= ual return type is a Double. Now, I can imagine scripts where you randomly = pick from any class on the classpath, invoke a no-args constructor, and ret= urn that... in that case I don't expect magic. But most scripts have on= e or two possible return statements (maybe implicit), I'm wondering if = there is already code that can statically analyse a method/script and say t= hat there are only one, two or whatever possible return types.

I'l= l take a look at ReturnAdder, thanks for that.

cheers, jamie


= On Mon, Sep 21, 2015 at 11:55 PM, Shil Sinha <shil.sinha@gmail.com= > wrote:
Actua= lly, the second option can be made safe by initializing ReturnAdder with a = listener, which will allow you to 'visit' all implicit returns with= out actually adding return statements.

On Mon, Sep 21, 2015 at 5:30 PM, Shil = Sinha <shil.sinha@gmail.com> wrote:
You could call 'getInferredReturnType= 9; on the 'run' method node of your script class, but inferred retu= rn types only appear to be stored for closures and certain binary expressio= ns (based on the usages of the INFERRED_RETURN_TYPE marker and the storeInf= erredReturnType method).

You could also add all implicit= return statements to the 'run' method via ReturnAdder, and then in= fer a return type by visiting all of the return statements, but I'm not= sure if that kind of AST modification in a type checking extension is safe= .=C2=A0

Shil

On Mon, Sep 21, 2015 at 4:34 PM, Jamie Echlin = <jamie.echli= n@gmail.com> wrote:
Hi...

Is it possible to infer the return type, or types of a scri= pt during static compilation? Presumably there is always a finite list of b= ranches where the script could end.

I'm looking at=C2=A0org.codeha= us.groovy.transform.stc.StaticTypeCheckingVisitor#getInferredReturnType, bu= t I'm not really sure how to call it from a type checking script, nor w= hat adds the inferred return type to the metadata which is used in that met= hod.

cheers, jamie





--e89a8f647253ff942c052052bc9f--