comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: User defined implicit conversion
Date: Wed, 30 Oct 2019 16:26:12 -0500
Date: 2019-10-30T16:26:12-05:00	[thread overview]
Message-ID: <qpcv5l$sm7$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: d9269cae-93e5-428c-9c7c-a7a1f5679806@googlegroups.com

"Alain De Vos" <devosalain71@gmail.com> wrote in message 
news:d9269cae-93e5-428c-9c7c-a7a1f5679806@googlegroups.com...
> Casting from one type to another is a real pain.
> Does the functionality "User defined implicit conversion" exists?
> If not are there strategies, general rules, to limit conversion to a 
> minimum ?
>
> We don't want as concatination :
> result := to_type_a (from_type_a (X) & from_type_a (Y)) ;
> when you could have :
> result := X & Y ;

The only reason you would have that is if you forgot to define "&" for 
Type_A. Depending on the definition of Type_A, that's unlikely to happen 
automatically.

Ergo, adding
      function "&" (Left, Right : Type_A) return Type_A is
              (to_type_a (from_type_a (X) & from_type_a (Y)));

Solves the problem in typical use.

Implicit conversion itself is the same is eliminating strong typing. If you 
really want that, you probably need to use subtypes rather than types. That 
is, "Type_A" should be a subtype rather than a type.

For example, instead of:

     type Type_A is new String;

use:

     Subtype Type_A is String;

And now you never need to use a type conversion between Type_A and String.

                                           Randy.




  parent reply	other threads:[~2019-10-30 21:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-30 19:43 User defined implicit conversion Alain De Vos
2019-10-30 19:58 ` Simon Wright
2019-10-30 20:39 ` Egil H H
2019-10-30 21:26 ` Randy Brukardt [this message]
2019-10-30 22:17   ` Dmitry A. Kazakov
2019-11-06  6:19   ` G. B.
2019-10-31 11:18 ` AdaMagica
replies disabled

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