|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 | Next > |
|
|
castHi,
I'm new to groovy.
I'm an experienced perl developer.
I like simple concise syntax, now I want switch to groovy that I wish will be the true Perl 6. Just a first big problem.
I'm expecting from Groovy some sort of obvious types casting.
Why this does not work as expected ?
(I expect an automatic cast to the left operant type.... so 1+'1' == 2)
def a = '1';
println 1+a;
Why this more verbouse does not work too ?
def a = '1';
println 1+(Integer)a; I'm really forced to use this orrible syntax, just to convert a string to a number !?
def a = '1';
println 1+Integer.parseInt(a); thanks
|
|||||
|
|
Re: castThis is a common mistake for people coming from Perl. Perl is weakly
typed, but Groovy is strongly typed like Java, meaning that even though you use def a = '1', the underlying type is still a String You have to convert the type for your code to work as you want, such as println 1+a.toInteger() Cheers On Mon, Jun 30, 2008 at 10:45 AM, Francesco Pasqualini <frapas@...> wrote: > Hi, > I'm new to groovy. > I'm an experienced perl developer. > I like simple concise syntax, now I want switch to groovy that I wish will > be the true Perl 6. > > Just a first big problem. > I'm expecting from Groovy some sort of obvious types casting. > Why this does not work as expected ? > (I expect an automatic cast to the left operant type.... so 1+'1' == 2) > > def a = '1'; > println 1+a; > > > Why this more verbouse does not work too ? > > def a = '1'; > println 1+(Integer)a; > > I'm really forced to use this orrible syntax, just to convert a string to a > number !? > > def a = '1'; > println 1+Integer.parseInt(a); > > > thanks > -- Graeme Rocher Grails Project Lead G2One, Inc. Chief Technology Officer http://www.g2one.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|||||
|
|
Re: castAnd don't feel too bad because even amongst hard core strongly typed advocates there is still STRONG debate about how far we can stretch the language to make things such as what you were trying to do easier to do but still remain safe. Cheers, Paul. Graeme Rocher wrote: > This is a common mistake for people coming from Perl. Perl is weakly > typed, but Groovy is strongly typed like Java, meaning that even > though you use def a = '1', the underlying type is still a String > > You have to convert the type for your code to work as you want, such as > > println 1+a.toInteger() > > Cheers > > On Mon, Jun 30, 2008 at 10:45 AM, Francesco Pasqualini <frapas@...> wrote: >> Hi, >> I'm new to groovy. >> I'm an experienced perl developer. >> I like simple concise syntax, now I want switch to groovy that I wish will >> be the true Perl 6. >> >> Just a first big problem. >> I'm expecting from Groovy some sort of obvious types casting. >> Why this does not work as expected ? >> (I expect an automatic cast to the left operant type.... so 1+'1' == 2) >> >> def a = '1'; >> println 1+a; >> >> >> Why this more verbouse does not work too ? >> >> def a = '1'; >> println 1+(Integer)a; >> >> I'm really forced to use this orrible syntax, just to convert a string to a >> number !? >> >> def a = '1'; >> println 1+Integer.parseInt(a); >> >> >> thanks >> > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|||||
|
|
Re: castOk
thanks
1+a.toInteger()
is quite good syntax for me. But why groovy in
1+'1' == '11'
does implicit conversion to String?
This seem a bit arbitrary to me.
I would prefer an implicit conversion to the left operand type, or no conversion at all.
On Mon, Jun 30, 2008 at 11:48 AM, Graeme Rocher <graeme@...> wrote: This is a common mistake for people coming from Perl. Perl is weakly |
|||||
|
|
Re: castSo I enter the debate. :-)
I think implicit conversion (for example from string to numbers) would give to groovy more usability as Pratical Exatrction and Report Language.
On Mon, Jun 30, 2008 at 11:56 AM, Paul King <paulk@...> wrote:
|
|||||
|
|
Re: castOn Mon, Jun 30, 2008 at 11:08 AM, Francesco Pasqualini <frapas@...> wrote:
> So I enter the debate. :-) > I think implicit conversion (for example from string to numbers) would give > to groovy more usability as Pratical Exatrction and Report Language. It isn't arbitrary, Groovy is simply following Java's rules with regards to string concatentation ie. whenever you use + to add something where one side is a String you get another String And if Groovy were to be more like Perl here it would be very surprising for Java developers (ie. the main target audience of the language) Cheers > > > > On Mon, Jun 30, 2008 at 11:56 AM, Paul King <paulk@...> wrote: >> >> And don't feel too bad because even amongst hard core strongly >> typed advocates there is still STRONG debate about how far we >> can stretch the language to make things such as what you were >> trying to do easier to do but still remain safe. >> >> Cheers, Paul. >> >> Graeme Rocher wrote: >>> >>> This is a common mistake for people coming from Perl. Perl is weakly >>> typed, but Groovy is strongly typed like Java, meaning that even >>> though you use def a = '1', the underlying type is still a String >>> >>> You have to convert the type for your code to work as you want, such as >>> >>> println 1+a.toInteger() >>> >>> Cheers >>> >>> On Mon, Jun 30, 2008 at 10:45 AM, Francesco Pasqualini <frapas@...> >>> wrote: >>>> >>>> Hi, >>>> I'm new to groovy. >>>> I'm an experienced perl developer. >>>> I like simple concise syntax, now I want switch to groovy that I wish >>>> will >>>> be the true Perl 6. >>>> >>>> Just a first big problem. >>>> I'm expecting from Groovy some sort of obvious types casting. >>>> Why this does not work as expected ? >>>> (I expect an automatic cast to the left operant type.... so 1+'1' == 2) >>>> >>>> def a = '1'; >>>> println 1+a; >>>> >>>> >>>> Why this more verbouse does not work too ? >>>> >>>> def a = '1'; >>>> println 1+(Integer)a; >>>> >>>> I'm really forced to use this orrible syntax, just to convert a string >>>> to a >>>> number !? >>>> >>>> def a = '1'; >>>> println 1+Integer.parseInt(a); >>>> >>>> >>>> thanks >>>> >>> >>> >>> >> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> > > -- Graeme Rocher Grails Project Lead G2One, Inc. Chief Technology Officer http://www.g2one.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|||||
|
|
Re: castOn Mon, Jun 30, 2008 at 12:09 PM, Graeme Rocher <graeme@...> wrote:
ok.
not shure but i think that a great target for groovy are many developers tired of Java syntax rules like this one.
|
|||||
|
|
Re: castI tried also this syntax ( without success )
def a = '1'
println 1+ a as int But as I read now in the archive, this seem just a bug. It's correct ?
On Mon, Jun 30, 2008 at 11:48 AM, Graeme Rocher <graeme@...> wrote: This is a common mistake for people coming from Perl. Perl is weakly |
|||||
|
|
Re: castYes it's a bug.
On Mon, Jun 30, 2008 at 12:48 PM, Francesco Pasqualini <frapas@...> wrote: > I tried also this syntax ( without success ) > > def a = '1' > println 1+ a as int > But as I read now in the archive, this seem just a bug. It's correct ? > > http://www.nabble.com/String-as-Integer-td18116618.html > > thanks > > On Mon, Jun 30, 2008 at 11:48 AM, Graeme Rocher <graeme@...> wrote: >> >> This is a common mistake for people coming from Perl. Perl is weakly >> typed, but Groovy is strongly typed like Java, meaning that even >> though you use def a = '1', the underlying type is still a String >> >> You have to convert the type for your code to work as you want, such as >> >> println 1+a.toInteger() >> >> Cheers >> >> On Mon, Jun 30, 2008 at 10:45 AM, Francesco Pasqualini <frapas@...> >> wrote: >> > Hi, >> > I'm new to groovy. >> > I'm an experienced perl developer. >> > I like simple concise syntax, now I want switch to groovy that I wish >> > will >> > be the true Perl 6. >> > >> > Just a first big problem. >> > I'm expecting from Groovy some sort of obvious types casting. >> > Why this does not work as expected ? >> > (I expect an automatic cast to the left operant type.... so 1+'1' == 2) >> > >> > def a = '1'; >> > println 1+a; >> > >> > >> > Why this more verbouse does not work too ? >> > >> > def a = '1'; >> > println 1+(Integer)a; >> > >> > I'm really forced to use this orrible syntax, just to convert a string >> > to a >> > number !? >> > >> > def a = '1'; >> > println 1+Integer.parseInt(a); >> > >> > >> > thanks >> > >> >> >> >> -- >> Graeme Rocher >> Grails Project Lead >> G2One, Inc. Chief Technology Officer >> http://www.g2one.com >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> > > -- Guillaume Laforge Groovy Project Manager G2One, Inc. Vice-President Technology http://www.g2one.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|||||
|
|
Re: castsuch a bug show that you are right, the main target are java developer... they use java syntax in groovy
Anyway I whish (if possible) groovy be more perlish, still remain java compatible ...
On Mon, Jun 30, 2008 at 12:50 PM, Guillaume Laforge <glaforge@...> wrote: Yes it's a bug. |
|||||
|
|
Re: castIt's difficult to find the right mix sometimes.
What feels natural for some, can feel counter-intuitive for others. In this case, it seems a majority is coming from a Java background and feels better with our current convention. But I can understand that coming from Perl, it may not seem as the right convention to use, I'm afraid. On Mon, Jun 30, 2008 at 12:59 PM, Francesco Pasqualini <frapas@...> wrote: > such a bug show that you are right, the main target are java developer... > they use java syntax in groovy > > Anyway I whish (if possible) groovy be more perlish, still remain java > compatible ... > > On Mon, Jun 30, 2008 at 12:50 PM, Guillaume Laforge <glaforge@...> > wrote: >> >> Yes it's a bug. >> >> On Mon, Jun 30, 2008 at 12:48 PM, Francesco Pasqualini <frapas@...> >> wrote: >> > I tried also this syntax ( without success ) >> > >> > def a = '1' >> > println 1+ a as int >> > But as I read now in the archive, this seem just a bug. It's correct ? >> > >> > http://www.nabble.com/String-as-Integer-td18116618.html >> > >> > thanks >> > >> > On Mon, Jun 30, 2008 at 11:48 AM, Graeme Rocher <graeme@...> >> > wrote: >> >> >> >> This is a common mistake for people coming from Perl. Perl is weakly >> >> typed, but Groovy is strongly typed like Java, meaning that even >> >> though you use def a = '1', the underlying type is still a String >> >> >> >> You have to convert the type for your code to work as you want, such as >> >> >> >> println 1+a.toInteger() >> >> >> >> Cheers >> >> >> >> On Mon, Jun 30, 2008 at 10:45 AM, Francesco Pasqualini >> >> <frapas@...> >> >> wrote: >> >> > Hi, >> >> > I'm new to groovy. >> >> > I'm an experienced perl developer. >> >> > I like simple concise syntax, now I want switch to groovy that I >> >> > wish >> >> > will >> >> > be the true Perl 6. >> >> > >> >> > Just a first big problem. >> >> > I'm expecting from Groovy some sort of obvious types casting. >> >> > Why this does not work as expected ? >> >> > (I expect an automatic cast to the left operant type.... so 1+'1' == >> >> > 2) >> >> > >> >> > def a = '1'; >> >> > println 1+a; >> >> > >> >> > >> >> > Why this more verbouse does not work too ? >> >> > >> >> > def a = '1'; >> >> > println 1+(Integer)a; >> >> > >> >> > I'm really forced to use this orrible syntax, just to convert a >> >> > string >> >> > to a >> >> > number !? >> >> > >> >> > def a = '1'; >> >> > println 1+Integer.parseInt(a); >> >> > >> >> > >> >> > thanks >> >> > >> >> >> >> >> >> >> >> -- >> >> Graeme Rocher >> >> Grails Project Lead >> >> G2One, Inc. Chief Technology Officer >> >> http://www.g2one.com >> >> >> >> --------------------------------------------------------------------- >> >> To unsubscribe from this list, please visit: >> >> >> >> http://xircles.codehaus.org/manage_email >> >> >> >> >> > >> > >> >> >> >> -- >> Guillaume Laforge >> Groovy Project Manager >> G2One, Inc. Vice-President Technology >> http://www.g2one.com >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> > > -- Guillaume Laforge Groovy Project Manager G2One, Inc. Vice-President Technology http://www.g2one.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|||||
|
|
Re: castOn Mon, 2008-06-30 at 12:59 +0200, Francesco Pasqualini wrote:
> Anyway I whish (if possible) groovy be more perlish, still remain java > compatible ... I suspect you are going to be disappointed. Each programming language has its computational model and its look and feel. So Perl, Python, Ruby, Groovy all look and feel different. Groovy's aim is to be Java's dynamic partner, so the underlying model is to be directly and explicitly compatible with Java at the bytecode level. Systems such as Jython and JRuby bring the Python and Ruby computational models respectively to the JVM. In order to bring the Perl computational model to the JVM, someone will have to write JPerl, i.e. the Perl language runing on the JVM -- as opposed to Jperl which is a C++ package for allowing Java code to use Perl. Whilst there are similarities between Perl and Groovy, Groovy is never going to be Perl running on the JVM. So without JPerl available your best bet is to use Groovy as it is meant to be used. Coming from Perl there are various idioms of use for achieving certain algorithmic intents. In a lot of case there are direct equivalents in Groovy, but not always. In these cases it is best to use the Groovy idioms rather than try and coerce Groovy to be more like Perl. The same applied for any Groovy programmers using Perl, just in reverse. The summary here is, think in terms of algorithms and then map down to the language being used, using the natural idioms for that language. I hope this helps. -- Russel. ==================================================== Dr Russel Winder Partner Concertant LLP t: +44 20 7585 2200, +44 20 7193 9203 41 Buckmaster Road, f: +44 8700 516 084 London SW11 1EN, UK. m: +44 7770 465 077 |
|||||
|
|
Re: castok I agree.
I don't want perl in jvm. I just like concise syntax and I think this is a groovy feature. Or not ?
Anyway, now that I know that I can use "as int" to convert a string to Integer I'm less worried.
It could be usefull an operator to force a numeric context. In Perl6 (just to grab an idea ) there is an operator (+) that force a numeric context.
println +a + 2;
give:
- if a is an array: the number of elements of a more 2
- if a is a string: the value (integer or double as appropriate) of a more 2
- if a is an hash: the number of elements
+++a increment the numeric value of a
Why don't introduce in groovy a single char operator to force a numeric contex ?
<<
Numeric Context
Arrays used in numeric context return their size, as in Perl 5. Perl 6 uses the "+" prefix or "num", "int", or "float" keywords to force numeric context. We don't have those keywords (yet), but "+" and "scalar" do the trick for now. Numeric context is also supplied by math related operators such as "-", "*", "sin", and so on. Force numeric context to get the old Perl 5 behavior of counting the elements in an array or hash: scalar @arr; 0 + @arr; In Perl 6, the 0 is redundant and undesireably ugly but it is required for our purposes so I suggest using "scalar" instead. >> On Mon, Jun 30, 2008 at 1:14 PM, Russel Winder <russel.winder@...> wrote:
|
|||||
|
|
Re: castThen you have curmudgeonly old people like me who are profoundly
uncomfortable with how loosely Groovy plays with types and would like to have something equivalent to "strict" in perl to keep variables from being automatically entered in the global binding to help catch typos. Or would like some way of saying "I should be able to specify "don't allow code to compile if there isn't a method signature in this class that matches exactly"... Overall, my biggest discomfort with Groovy is how cavalier it is about throwing runtime errors. I want the compiler to help me as much as possible. That said, I'm really enjoying the grails/groovy experience, it's waaaay cool. I suppose most of my issues are just getting accustomed to a new way of doing things. I suspect that I'm also mis-using Groovy in many ways, and as I get better at using Groovy for its niche and Java for its niche I'll get less uneasy. At the end of the day, I suppose the real measure is how much faster we're getting the applications out the door. And it's going really well here. The last thing I'd like to see is Groovy to become a hash of unrelated idioms because it's trying to be all things to all people. I'd far rather it be a coherent set of idioms (even if those idioms cause me grief. I'll adjust) than exhibit inconsistent behavior trying to be some sort of language with all the features of all the languages that exist OK, enough already. I need to get to work <G>. Best Erick P.S. If anybody has any links to "Groovy for crusty old Java (and C and C++) programmers, please let me know.... Best Erick On Mon, Jun 30, 2008 at 8:07 AM, Francesco Pasqualini <frapas@...> wrote:
|
|||||
|
|
Re: castNot sure if you've seen these. They helped me become a little more comfortable: http://pleac.sourceforge.net/pleac_groovy/ http://blog.hhhhq.org/2007/04/ftp-using-groovy-and-ant.html (pretty cool Antbuilder stuff) Book: Goovy Recipes - Greasing the Wheels of Java by Scott Davis
|