jastrachan | 7 Apr 10:32 2004
Picon

[IDEA] auto-type coercion thoughts

I've mused about this on and off at times over the last few months and 
Sam brought it up on IRC a little while ago so thought I'd mention it 
again...

There's lots of fiddly methods to handle URL / File / InputStream / 
Reader etc. I've wondered if we should support some kind of 
auto-casting feature that types can support if they wish.

e.g.

class Object {
     toType(Class type) {
	if (type == String) {
	    return toString()
	}
	return null
     }
}

then someone could do more clever implementations

class Integer extends Number {
    toType(Class type) {
	switch (type) {
	    case Long:
		return longValue()
	    case Double:
		return doubleValue()
	}
	return super.toType(type)
     }
}

Similarly we could allow things like File / String / URL to be coerced 
into a Reader / InputStream.

In terms of DefaultGroovyMethods - there's lots of redundancy. 
Particularly for the IO methods. We kinda wanna write lots of methods 
in term of (say) Reader and then mixin those methods to types like 
File, URL, InputStream, String without having to code all the 
implementations.

This would allow types to decide what things they can be coerced into. 
e.g. allowing arrays and List to be auto-coerced if required.

Thoughts?

James
-------
http://radio.weblogs.com/0112098/

Gmane