comp.lang.ada
 help / color / mirror / Atom feed
* User defined implicit conversion
@ 2019-10-30 19:43 Alain De Vos
  2019-10-30 19:58 ` Simon Wright
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Alain De Vos @ 2019-10-30 19:43 UTC (permalink / raw)


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 ;


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: User defined implicit conversion
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Simon Wright @ 2019-10-30 19:58 UTC (permalink / raw)


Alain De Vos <devosalain71@gmail.com> writes:

> 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 ;

   function "&" (L : Type_X, R : Type_Y) return Type_A;
   function "&" (L : Type_Y, R : Type_X) return Type_A;

which means that the tiresome manipulation only has to happen in one
place. Well, two places, unless you say

   function "&" (L : Type_Y, R : Type_X) return Type_A
   is (R & L);


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: User defined implicit conversion
  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
  2019-10-31 11:18 ` AdaMagica
  3 siblings, 0 replies; 7+ messages in thread
From: Egil H H @ 2019-10-30 20:39 UTC (permalink / raw)


On Wednesday, October 30, 2019 at 8:43:05 PM UTC+1, Alain De Vos wrote:
> 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 ;


If they are all type_a, as indicated in the example, 
you should be able to do just
result := X & Y;

Maybe you forgot to make the operator visible?
use type type_a;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: User defined implicit conversion
  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
  2019-10-30 22:17   ` Dmitry A. Kazakov
  2019-11-06  6:19   ` G. B.
  2019-10-31 11:18 ` AdaMagica
  3 siblings, 2 replies; 7+ messages in thread
From: Randy Brukardt @ 2019-10-30 21:26 UTC (permalink / raw)


"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.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: User defined implicit conversion
  2019-10-30 21:26 ` Randy Brukardt
@ 2019-10-30 22:17   ` Dmitry A. Kazakov
  2019-11-06  6:19   ` G. B.
  1 sibling, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2019-10-30 22:17 UTC (permalink / raw)


On 2019-10-30 22:26, Randy Brukardt wrote:

> Implicit conversion itself is the same is eliminating strong typing.

It depends, but arbitrary conversions certainly do.

> 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.

This is a restriction imposed by Ada 83 in order to be able to generate 
such null-conversions. Actually it is not null, a subtype can add a 
constraint and then the conversion must include constraint check.

A more general case is when a subtype can have an independent 
representation and user-defined conversions:

    subtype UTF8_String is String;
       -- The conversion must recode Latin-1 to UTF-8

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: User defined implicit conversion
  2019-10-30 19:43 User defined implicit conversion Alain De Vos
                   ` (2 preceding siblings ...)
  2019-10-30 21:26 ` Randy Brukardt
@ 2019-10-31 11:18 ` AdaMagica
  3 siblings, 0 replies; 7+ messages in thread
From: AdaMagica @ 2019-10-31 11:18 UTC (permalink / raw)


Am Mittwoch, 30. Oktober 2019 20:43:05 UTC+1 schrieb Alain De Vos:
>  are there strategies, general rules, to limit conversion to a minimum ?

Yes, a good design. If you need many type conversions, there definitely is something wrong in your design.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: User defined implicit conversion
  2019-10-30 21:26 ` Randy Brukardt
  2019-10-30 22:17   ` Dmitry A. Kazakov
@ 2019-11-06  6:19   ` G. B.
  1 sibling, 0 replies; 7+ messages in thread
From: G. B. @ 2019-11-06  6:19 UTC (permalink / raw)


Randy Brukardt <randy@rrsoftware.com> wrote:
> "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.

> 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.

Implicit conversion introduces implicit knowledge, shared by authors of a
program. So, weak types should help employees stay safe in their jobs if
they have the knowledge.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-11-06  6:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2019-10-30 22:17   ` Dmitry A. Kazakov
2019-11-06  6:19   ` G. B.
2019-10-31 11:18 ` AdaMagica

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