By reflection I mean the sort of thing Java does in java.lang.reflect



i.e. given the name/type  of a function we can retrieve that function and
subsequently use it
in the same way that in java given the name of a class we can create
instances of that class.

i.e. it is quite common to have dialog which accepts a filename looks at the
suffix
turns that in to class name and picks up the class. That class knows how to
deal with files of that type and all classes share a common interface.

The sort of problem I'm describing is something I would try to use the
Factoy design pattern for nin OO speak (GoF Design Patterns : Elements of
Reusable Object-Oriented Software).



In Haskell we have 

module Foo
import Bmp
...
        ... Bmp.read filename

i.e. we can only call the functions inside modules we have imported (in this
case Foo can read bitmaps because it imports Bmp) 

However if we wanted to extend Foo's functionality so that it could read
Gifs as well
we would have to edit Foo

module Foo
import Bmp
import Gif
...
        ... Bmp.read filename

        ... Gif.read filename

etc

and for each new format we wanted to add we would edit Foo.hs ... yeugh!

if we had a mechanism for retrieving functions then this would not be
necessary

we could inspect the filename 
if it was a .gif then retrieve Gif.read
if it was a .bmp then retrieve Bmp.read
if it was a .foo then retrieve Foo.read
etc

now Foo's functionality may be extended in an additive rather than editive
way


module Foo
import Reflect 
 ...    let 
            fn = Reflect.getFn (bottom::String -> ImageReader) name
          name = suffix+ ".read"
            suffix = getSuffix filename
        in 
                case fn of
                        Nothing -> error ("Function that knows how to read "
+ suffix +" files does not exist")
                        Just f  -> f filename





-----Original Message-----
From: Peter Hancock [mailto:[EMAIL PROTECTED]]
Sent: 25 January 2000 11:13
To: Chris Angus
Subject: Relection


>>>>> "Chris" == Chris Angus <[EMAIL PROTECTED]> writes:

    > I posted this to comp.lang.functional a while ago but (apparently) no
one
    > had an opinion which
    > I cant believe is the case :-)

Hi Chris.  The problem is likely to be that (like me) many people
don't know what you mean by "reflection" -- I couldn't make it 
out from your message.  [I know what reflection means in set
theory, but (pardon my ignorance) I haven't heard of it in FP.]

Maybe you could post a URL for a paper which explains this 
concept well? 

Regards,

Peter

Reply via email to