Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Jon's many-language ray tracer
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andreas Rossberg  
View profile  
 More options Jun 29 2005, 8:47 am
Newsgroups: comp.lang.functional, comp.lang.java.programmer
From: Andreas Rossberg <rossb...@ps.uni-sb.de>
Date: Wed, 29 Jun 2005 14:47:43 +0200
Local: Wed, Jun 29 2005 8:47 am
Subject: Re: Jon's many-language ray tracer

Chris Rathman wrote:

> My rule of thumb is that if it can handle the shapes example, then it's
> an object oriented programming language.  About as objective as any
> other definition of what OOP means.  The downside is that this puts
> BrainF*ck into the OO PL camp.  :-)

> http://www.angelfire.com/tx4/cus/shapes/index.html

Yes, and doesn't the size of this table hence make precisely my point,
i.e. that such a broad definition of OO is utterly useless? ;-)

> (Still mulling how to best do the example for Alice ML).

I don't hesitate at all to concede that Alice ML is not an
object-oriented language. :-)

Having said that, as the page's problem description stands, it is
straightforward to solve with ML modules:

   signature SHAPE =
   sig
      val draw    : unit -> unit
      val moveTo  : int * int -> unit
      val rMoveTo : int * int -> unit
   end

   functor Shape (val x:int val y:int) =
   struct
      val x = ref x and y = ref y
      fun moveTo (x', y') = (x := x'; y := y')
      fun rMoveTo (dx, dy) = (x := !x+dx; y := !y+dy)
   end

   functor Circle (val x:int val y:int val r:int) =
   struct
      structure Shape = Shape (val x=x val y=y)
      open Shape
      val r = ref r
      fun setRadius r' = r := r'
      fun draw () = ...
   end

   functor Rectangle (val x:int val y:int val w:int val h:int) =
   struct
      structure Shape = Shape (val x=x val y=y)
      open Shape
      val ref w and h = ref h
      fun setWidth w' = w := w'
      fun setHeight h' = h := h'
      fun draw () = ...
   end

   functor DoSomething (S : SHAPE) =
   struct
      val _ = (S.draw (); S.moveTo (100, 100); S.draw ())
   end

   structure R = Rectangle (val x=10 val y=20 val w=5 val h=6)
   structure C = Circle (val x=15 val y=25 val r=8)

   structure _ = DoSomething R
   structure _ = DoSomething C

Note that the problem description does not explicitly require shapes to
be first-class ;-). Also note that I even mimicked inheritance.

Now, if you really need shapes first-class, then you need first-class
modules. In Alice ML you can abuse packages for this, but you loose some
static typing (with static first-class modules like in Moscow ML you
wouldn't):

   signature RECTANGLE =
   sig
      include SHAPE
      val setWidth  : int -> unit
      val setHeight : int -> unit
   end

   signature CIRCLE =
   sig
      include SHAPE
      val setRadius : int -> unit
   end

   val r = pack R : RECTANGLE
   val c = pack C : CIRCLE

   fun doSomething p =
   let
      structure S = unpack p : SHAPE
   in
      S.draw (); S.moveTo (100, 100); S.draw ()
   end

   val _ = List.map doSomething [r, c]

Does that suit you?

Caveat: all code untested.

   - Andreas

--
Andreas Rossberg, rossb...@ps.uni-sb.de

Let's get rid of those possible thingies!  -- TB


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google