groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <>
Subject Re: Casting Double to BigDecimal
Date Tue, 12 Jan 2016 12:07:13 GMT
Am 12.01.2016 um 11:01 schrieb Alberto Vilches:
> Thank you! Now I know the difference between these two operators :) I
> always though the Java-like cast do an "as type" behind the scenes. I
> think is kind of confusing to have theoretically equivalent operator
> with differents results.

One more: The main difference is that "as" will allow the user to supply 
custom casts, while the normal cast is defined by the language. So for 
example I can do the following

class Foo {
   def asType(Class c) {
     if (c==Bar) return new Bar(inner:this)
     return super.asType(c) // call Object.asType(Class)
class Bar {
   def inner

def f = new Foo()
def b = f as Bar
assert b.inner == f

And of course it could be a much more complex option, plus you can 
provide default methods to allow for such conversions.

> Deeping in the code I have another differents
> like [] as Set creates a LinkedHashSet and (Set)[] creates a HashSet.
> Anyway, I don't mind the current behavior, but I really think it should
> be documented (before to write the question in the mail list, I tried to
> find in the docs with no results). Thank you again for your time, it's
> really appreciated.

It is mentioned under

Maybe the section is a bit too short, but it is mentioned ;)

bye Jochen

View raw message