From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!attcan!uunet!amdahl!ames!pasteur!ucbvax!skvax1.csc.ti.com!linnig From: linnig@skvax1.csc.ti.com (Mike Linnig) Newsgroups: comp.lang.ada Subject: RE: Dynamic Typing in Ada Message-ID: <8903101703.AA04518@ti.com> Date: 10 Mar 89 16:49:27 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet List-Id: 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!