Implicit type conversion

From Rosetta Code
Revision as of 05:17, 23 April 2013 by rosettacode>NevilleDNZ (wp:Implicit type conversion)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Implicit type conversion is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Some programming languages have Implicit type conversion(wp). Type conversion is also known as coercion.

For example: <lang algol68>COMPL z := 1;</lang>Here the assignment ":=" implicitly converts the integer 1, to the complex number in the programming language ALGOL 68.

The alternative would be to explicitly convert a value from one type to another, using a function or some other mechanism (e.g. an explicit cast).

The following code sample demonstrates the various type conversions in each language, and gives an example of a implicit type conversion path from the smallest possible variable size to the largest possible variable size. (Where the size of the underlying variable's data strictly increases).

In strong typed languages some types actually mutually incompatible. In this case the language may have disjoint type conversion paths, or even branching type conversion paths. (Where it occurs in a specific language, it is demonstrated in the code samples below.)

Languages that don't support any implicit type conversion are detailed in the /Omit categories at the bottom of this page.

Indicate if the language supports user defined type conversion definitions. And give an example of such a definition. (E.g. define an implicit type conversion from real to complex numbers, or from char to an array of char of length 1.)

C

<lang c>#include <stdio.h> main(){ /* a representative sample of builtin types */

   unsigned char uc; char c;
   enum{e1, e2, e3}e123;
   short si; int i; long li;
   unsigned short su; unsigned u; unsigned long lu;
   float sf; float f; double lf; long double llf;
   union {char c; unsigned u; int i; float f; }ucuif;  /* manual casting only */
   struct {char c; unsigned u; int i; float f; }scuif; /* manual casting only */
   int ai[99];
   int (*rai)[99];
   int *ri;
   uc = '1';

/* a representitive sample of implied casts for subtypes of personality int/float */

   c=uc;
   si=uc; si=c;
   su=uc; su=c; su=si;
   i=uc;  i=c;  i=si;  i=su;
   e123=i; i=e123;
   u=uc;  u=c;  u=si;  u=su;  u=i;
   li=uc; li=c; li=si; li=su; li=i; li=u;
   lu=uc; lu=c; lu=si; lu=su; lu=i; lu=u; lu=li;
   sf=uc; sf=c; sf=si; sf=su; sf=i; sf=u; sf=li; sf=lu;
   f=uc;  f=c;  f=si;  f=su;  f=i;  f=u;  f=li;  f=lu;  f=sf;
   lf=uc; lf=c; lf=si; lf=su; lf=i; lf=u; lf=li; lf=lu; lf=sf; lf=f;
   llf=uc;llf=c;llf=si;llf=su;llf=i;llf=u;llf=li;llf=lu;llf=sf;llf=f;llf=lf;

/* ucuif = i; no implied cast; try: iucuif.i = i */ /* ai = i; no implied cast; try: rai = &i */ /* a representitive sample of implied casts, type pointer */

   rai = ai; /* starts at the first element of ai */
   ri = ai;  /* points to the first element of ai */

/* a summary result, using the longest sizeof increasing casting path */

   printf("%LF was increasingly cast from %d from %d from %d from %d from %d bytes and was '%c'\n",
          llf=(lf=(i=(si=c))), sizeof llf, sizeof lf, sizeof i, sizeof si,sizeof c, c);

}</lang>