Adaだとどうするん?と思って適当にそれっぽいコードを書いたところ、普通にキャストすれば良さそうな感じ。*1
with Ada.Text_Io; procedure Dsp_Test is package Bs is type B is tagged null record; procedure Method(Obj: in out B); end Bs; package body Bs is procedure Method(Obj: in out B) is begin Ada.Text_Io.Put_Line("B#method"); end Method; end Bs; package Ds is type D is new Bs.B with null record; procedure Method(Obj: in out D); end Ds; package body Ds is procedure Method(Obj: in out D) is begin Ada.Text_Io.Put_Line("D#method"); end Method; end Ds; B:Bs.B'Class:=Ds.D'(Bs.B with null record); begin B.Method; --> D#method Bs.B(B).Method; --> B#method end Dsp_Test;
ドット記法使うと凄くOOPLっぽく見えますね…
ちなみに95方式で
Method(B); Method(Bs.B(B));
と書くと、Methodが見えないのでコンパイラに怒られますが、"use Bs, Ds;"しておけばドット記法を使った時と同様に解決してくれるようです。(この例では。)
*1:面倒くさくてARM確認してませんので規格的に正しいかは不明。