comp.lang.ada
 help / color / mirror / Atom feed
* RE: Dynamic Typing in Ada
@ 1989-03-10 16:49 Mike Linnig
  1989-03-19 21:52 ` Som Hane
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Linnig @ 1989-03-10 16:49 UTC (permalink / raw)


John Gateley's (Gateley@tilde.csc.ti.com) challenge was to implement the
following lisp construct in Ada...

 ----------------------------------------------------------------------
-- Scheme                           CL
-- (define print                    (defun print (x)
--   (lambda (x)                      (typecase x
--     (cond                            (integer 1)
--       ((integer? x) 1)               (real 2)
--       ((real? x) 2)                  (complex 3)
--       ((complex? x) 3)               (vector 4)))
--       ((vector? x) 4))))
 ----------------------------------------------------------------------
package dynamic_types is

  TYPE real IS NEW integer;             -- not how we would really do it
  TYPE complex IS NEW integer;          -- but good enuf for an example.
  TYPE vector IS NEW integer;

  Type Dynamic_Type_options is (integer_t,real_t,complex_t,vector_t);

  type Dynamic_Type(which: Dynamic_Type_options := integer_t) IS record
    CASE which IS
      WHEN integer_t    => I: integer;
      WHEN real_t       => R: real;
      WHEN complex_t    => C: complex;
      WHEN vector_t     => V: vector;
    END CASE;
  END RECORD;

 FUNCTION print(x: Dynamic_Type) return integer;
END dynamic_types;
 ----------------------------------------------------------------------
WITH text_io;
PACKAGE BODY  dynamic_types IS
   
   FUNCTION print(x: Dynamic_Type) return integer is
   BEGIN
      CASE x.which IS
         WHEN integer_t   => return (1);
         WHEN real_t      => return (2);
         WHEN complex_t   => return (3);
         WHEN vector_t    => return (4);
      END CASE;
   END print;  
      
END Dynamic_Types;
 ----------------------------------------------------------------------
WITH dynamic_types,text_io;
USE  dynamic_types;

PROCEDURE lisp_test IS
  PACKAGE int_io IS NEW text_io.integer_io(integer); -- I hate this.

  tmp1 : dynamic_type := (which=>real_t, r=> 3); -- init to real type
  
BEGIN
    text_io.put("the type number of tmp1 is :");
    int_io.put(print(tmp1)); 
    text_io.new_line;

    tmp1 := (which=>complex_t, c => 99);

    text_io.put("the NEW type number of tmp1 is :");
    int_io.put(print(tmp1)); 
    text_io.new_line;

END lisp_test;
 ----------------------- SAMPLE OUTPUT ---------------------------
the type number of tmp1 is :          2
the NEW type number of tmp1 is :          3
 ----------------------------------------------------------------------

I think this answers John Gateley's (Gateley@tilde.csc.ti.com) dynamic
typing challenge. I didn't understand the first challenge; if it involved
something similar to The C Language construct allowing pointers to
procedures (and calling them) that can be handled too.

	Mike Linnig,
	Texas Instruments

Geez, we work for the same company!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1989-03-24 18:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1989-03-10 16:49 Dynamic Typing in Ada Mike Linnig
1989-03-19 21:52 ` Som Hane
1989-03-24 18:42   ` Scott Moody

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox