Issue Details (XML | Word | Printable)

Key: GROOVY-174
Type: New Feature New Feature
Status: Closed Closed
Resolution: Won't Fix
Priority: Minor Minor
Assignee: Paul King
Reporter: Yuri Schimke
Votes: 1
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
groovy

Round Peg, Square Hole

Created: 28/Jan/04 09:58 AM   Updated: 09/Oct/07 07:31 PM
Component/s: groovy-jdk
Affects Version/s: 1.0
Fix Version/s: 1.1-rc-1

Time Tracking:
Not Specified


 Description  « Hide
Currently if you want to do something with a character stream i.e. Reader its common to provide multiple methods.

doSomething(Reader)
doSoemthing(File)
doSomething(URL)
doSomething(String)

And the File, URL, String methods will do extra operations to create a reader before calling doSomething(Reader).

The other option is to only have the reader method and make the caller perform those operations.

It would be nice if groovy could mash an object into another type to fit the parameters. i.e. If you called a method taking a Reader and you passed a File, it would create a new FileReader for you.

Maybe it looks for a method called toReader on the parameter?

Obviously this can create problems, like oversimplifying character encoding issues with readers etc. And may lead to bad APIs when called from Java. But its a timesaving feature. Any thoughts.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
james strachan added a comment - 28/Jan/04 10:03 AM
Interesting - might be worth adding a helper method toReader() somewhere; not sure if it should go on Object or if it should be a helper method elsewhere?

james strachan added a comment - 18/Feb/04 11:27 AM
Interesting stuff Corneil. We already support some basic type coercion mechanisms right now; like handling Enumeration / Iterator or arrays / collections and ints and Integer or GString and String.

Maybe we could go one step further and allow types to provide natural type coercion mechanisms.

e.g. we could add a method toType(Class) which would allow any type to offer its own type coercion mechanism to other types.

Then File, String, URL could all offer the ability to coerce themselves to an InputStream, Reader etc.

Then you could write a method

doSomething(Reader r)

and call it directly with a File, String, URL and so forth. (This would be neat!).

The one proviso with things like that is if you take a Reader / InputStream as a parameter you should always guarrentee to close it, whatever happens.


james strachan added a comment - 18/Feb/04 11:30 AM
this feature is a little like auto-casting or auto-coercion. In dynamic method dispatch mode, this should happen by default.

In static typing mode a cast is probably gonna be required.

e.g.

String x = "hello"
doSomething((Reader) x)


james strachan added a comment - 03/Mar/04 01:09 PM
While auto-coercion might be a little confusing, we've got heaps of helper methods now so if we all stick to Writer and Reader then we can do things like

doSomething(file.newReader())
doSomething(url.newReader())

etc


Guillaume Laforge added a comment - 24/Nov/04 11:45 AM
During the GroovyOne JSR meeting in London, we talked about using the "as" operator to provide a neater means to do that:

URL url = new URL("http://groovy.codehaus.org")
reader = url as Reader;
reader.readLine()


Paul King added a comment - 09/Oct/07 07:31 PM
Now done using as type as indicated in comments.