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 BCEAB186AB for ; Sun, 27 Dec 2015 16:55:02 +0000 (UTC) Received: (qmail 67158 invoked by uid 500); 27 Dec 2015 16:55:02 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 67124 invoked by uid 500); 27 Dec 2015 16:55:02 -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 67114 invoked by uid 99); 27 Dec 2015 16:55:02 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Dec 2015 16:55:02 +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 E7F1AC023A for ; Sun, 27 Dec 2015 16:55:01 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.12 X-Spam-Level: X-Spam-Status: No, score=-0.12 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001, URIBL_BLOCKED=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-us-east.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id AMM22cwwZ4SQ for ; Sun, 27 Dec 2015 16:54:56 +0000 (UTC) Received: from mail-wm0-f51.google.com (mail-wm0-f51.google.com [74.125.82.51]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with ESMTPS id 90036439B4 for ; Sun, 27 Dec 2015 16:54:55 +0000 (UTC) Received: by mail-wm0-f51.google.com with SMTP id l126so243846972wml.0 for ; Sun, 27 Dec 2015 08:54:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=N2x0TmRrZOtu5BiPHdOmF+RPDS8alysExnp0vKB3hg4=; b=sp1kKVEEimE6nToIcNqfJ+tdOa+kFiUBgwbSSPNuVEVztFQJ/Xnqi6a9EiK03tQuQ4 FkjvqUYLu+J5u1ILVjUrpwxPWV6nczaTH0w9y2fOHA6+A6sejBOscpbcMKg+FQkbr1Rp krU7HGMIxyE6Hiyq/meEyJ60SdrguK66Fti4izTNS7QOsugRxow7nPq598RwWnCGceyW +zyWAEzpOTzDcnKXL7Fy6vSxNOEfk86udSv7tv5O8ai6fxV7LJHd6XFtNHk1AeosSQIG jTkchzX5J85HglNqVREDK69dQzA7jslQI52pY7HOeFfB/SkPKa2325FK96nIuwXnArJS HxkA== X-Received: by 10.28.46.193 with SMTP id u184mr58366573wmu.102.1451235294586; Sun, 27 Dec 2015 08:54:54 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.178.162 with HTTP; Sun, 27 Dec 2015 08:54:35 -0800 (PST) In-Reply-To: <567FE96E.3050702@gmx.org> References: <5C82ED88-C0E8-40CC-9699-9F20036948A7@base2.cc> <567FE96E.3050702@gmx.org> From: =?UTF-8?B?RGlua28gU3Jrb8SN?= Date: Sun, 27 Dec 2015 17:54:35 +0100 Message-ID: Subject: Re: Problem overloading bracket operator with CompileStatic To: users@groovy.apache.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 27 December 2015 at 14:36, Jochen Theodorou wrote: > On 27.12.2015 09:18, Byron Foster wrote: >> >> Given the following code: >> >> =E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94= =E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2= =80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80= =94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94= =E2=80=94- >> package rankspin >> >> import groovy.transform.CompileStatic >> >> trait SimpleMap { >> abstract V getAt(K k) >> } >> >> class Foo implements SimpleMap { >> String getAt(String k) {return "bar"} >> } >> >> @CompileStatic >> void go(SimpleMap map) { >> println map["a"] >> } >> >> Foo foo =3D new Foo() >> go(foo) > > > so basically overloaded getAt in subclass plus static compilation and met= hod > call on the base class. Well... You have to keep in mind, that there is a > big difference in method dispatch when it comes to static and dynamic typ= ing > in Groovy. In dynamic Groovy, Groovy will take the runtime type of an obj= ect > to choose the method. In your case, the call site is the map["a"] aka > map.getAt("a") line. The static compiler will only see a SimpleMap. The > static compiler cannot know that this SimpleMap is maybe a Foo providing = a > better fitting getAt(String) method. And since the compiler cannot know > that, it will take the getAt(K) (aka getAt(Object)) method from SimpleMap= . > Even if you had tried SimpleMap, it would not have changed a thin= g. > > In other words, a static compiler like the one of Groovy (or the one of J= ava > and most other static languages cannot do this). Umm ... Jochen, are you sure this is right? I'm rather convinced that this should work in both Java (using interfaces instead of traits) and Scala ... and by extension, I would be inclined to call the above behaviour a bug. Being statically compiled or not, isn't this the case of dynamic dispatch, which should work in any OO language? Cheers, Dinko > > [..] >> >> Threw: >> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack >> msg: No such property: password for class: rankspin.web.MyRequest > >> >> >> With no stack trace. Even in the example code above it does not indicat= e >> the code site where the error occurred. is this correct? > > > This is not correct, no. The static compiler should not have used the > dynamic way to get a property, because that is was is producing the stack > less exception, which is normally caught before it reaches groovy code ag= ain > and transformed into a proper exception. Since the static compiler is not > really supposed to use those methods, it is no wonder the proper unwrappi= ng > is missing as well. > > bye Jochen >