groovy

Round Peg, Square Hole

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Won't Fix
  • Affects Version/s: 1.0
  • Fix Version/s: 1.1-rc-1
  • Component/s: groovy-jdk
  • Labels:
    None
  • Number of attachments :
    0

Description

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.

Activity

Hide
james strachan added a comment -

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?

Show
james strachan added a comment - 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?
Hide
james strachan added a comment -

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.

Show
james strachan added a comment - 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.
Hide
james strachan added a comment -

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)

Show
james strachan added a comment - 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)
Hide
james strachan added a comment -

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

Show
james strachan added a comment - 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
Hide
Guillaume Laforge added a comment -

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()

Show
Guillaume Laforge added a comment - 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()
Hide
Paul King added a comment -

Now done using as type as indicated in comments.

Show
Paul King added a comment - Now done using as type as indicated in comments.

People

Vote (1)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: