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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6daf1097e309f0a8,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-03-25 06:50:19 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!tar-alcarin.cbb-automation.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Disallowing methods [use for multiple dispatch] Date: Mon, 25 Mar 2002 15:50:16 +0100 Message-ID: NNTP-Posting-Host: tar-alcarin.cbb-automation.de (212.79.194.111) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: fu-berlin.de 1017067816 23786741 212.79.194.111 (16 [77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:21644 Date: 2002-03-25T15:50:16+01:00 List-Id: Hi all! Methods of tagged types cannot be disallowed in Ada. [For other types one can disallow predefined operations by making them abstract]. Here is an example when disallowing seem to be necessary for tagged types. Consider a dyadic primitive operation. It is a clear case for true multiple dispatch. Unfortunately, Ada 95 has no proper one [tags must be all same] . But there is a work-around. The following trick of cascading dispatch can be used to emulate true MD: package AA is type A is tagged null record; -- -- Dyadic operation. Note the second arg is not dispatching to avoid -- run-time errors. -- function "and" (X : A; Y : A'Class) return Boolean; end AA; ----------------------------------------- with Text_IO; use Text_IO; package body AA is function "and" (X : A; Y : A'Class) return Boolean is begin if Y in A then Put_Line ("A and A"); -- Do it here return True; else return Y and X; -- Dispatch on the second argument end if; end "and"; end AA; ----------------------------------------- with AA; use AA; package BB is type B is new A with null record; -- -- Overriding of the dyadic operation. BB is responsible to -- care of all cases except A and A. -- function "and" (X : B; Y : A'Class) return Boolean; end BB; ----------------------------------------- with Text_IO; use Text_IO; package body BB is function "and" (X : B; Y : A'Class) return Boolean is begin if Y in A then Put_Line ("B and A"); -- Do it here return True; elsif Y in B then Put_Line ("B and B"); -- Do it here return True; else return Y and X; -- Dispatch on the second argument end if; end "and"; end BB; ----------------------------------------- with AA; use AA; with BB; use BB; procedure Test is X : A; Y : B; Z : Boolean; begin Z := X and X; Z := Y and X; Z := X and Y; Z := Y and Y; end Test; ----------------------------------------- The above works fine printing as expected: A and A B and A B and A B and B But there is a great problem with the predefined equality operator "=". Change "and" to "=" and the above will not work, because function "=" (X : A; Y : A'Class) return Boolean; does not override the predefined function "=" (X, Y : A) return Boolean; Therefore both get overloaded! This is a case when an ability to disallow an operation would be very desirable. More generally it would be nice to have an explicit hidding construct for hidding. Like: function "=" (X, Y : A) return Boolean is null; The way limited vs. non-limited types is not enough flexible, IMO. Regards, Dmitry Kazakov