Challenge: solve this multiple inheritance problem in your favorite language

Simen Kjærås simen.kjaras at gmail.com
Thu Jun 4 09:37:38 UTC 2020


On Thursday, 4 June 2020 at 07:11:26 UTC, mw wrote:
> Problem:
>
> Suppose a person who has both US & UK residence, travel to 
> Paris, and feel ill need to withdraw some money and see a 
> doctor:
>
> 1) the person can only have 1 (one) name
> 2) the person has 3 addresses: one in US, one in UK, and a temp 
> hotel address in Paris
> 3) the person's bank account that can only be read by the bank
> 4) the person's health info that can only be read by doctor

This is all fairly reasonable, but why use multiple inheritance? 
I mean, it might be the logical way to do it in Eiffel, but in D 
that's just not the right way.

For that matter, it reads as a very artificial situation:
- What happens when the person buys a holiday home in Italy?
- Do we need to define a separate inheritance tree for all 
possible combinations?


Now, for showing off some of Eiffel's features, there's some good 
stuff here - the feature export system is kinda interesting, and 
doesn't really have a good analog in D, but may be approximated 
with non-creatable types:

module person;
import bank;
class Person {
     string bankingDetails(Bank.Token) {
         return "Account number readable only by Bank";
     }
}

module bank;
import person;
class Bank {
     struct Token {
         @disable this();
         private this(int i) {}
     }
     string personBankingDetails(Person person) {
         return person.bankingDetails(Token(0));
     }
}

module test;
import bank;
import person;
unittest {
     Person p = new Person();
     Bank b = new Bank();

     // Won't compile - only a Bank can create a Token
     //p.bankingDetails();

     // Works fine
     b.personBankingDetails(p);
}

--
   Simen


More information about the Digitalmars-d mailing list