It may be useful to allow declaring variables without type

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 10 10:52:40 PDT 2015


On 06/10/2015 10:49 AM, Atila Neves wrote:

 > On Wednesday, 10 June 2015 at 17:38:45 UTC, Binarydepth wrote:
 >> On Wednesday, 10 June 2015 at 17:32:18 UTC, Atila Neves wrote:
 >>> On Wednesday, 10 June 2015 at 17:04:20 UTC, Binarydepth wrote:
 >>>> I think that it could be useful to declare variables as Python does
 >>>> and statically too in the same language. Just make it's type when a
 >>>> value is assigned to the variable. This could allow a program to
 >>>> adjust it's data types accordingly to input when data is received
 >>>> from another program or an user.
 >>>>
 >>>> BD
 >>>
 >>> I disagree on the usefulness. I think this is one of the few things
 >>> Perl got right; the problem with automatically creating variables is
 >>> that you might create one when you're not expecting to because of a
 >>> typo.
 >>>
 >>> Atila
 >>
 >> Well, do think that can be solved by making the compiler report unused
 >> variables ?
 >>
 >> BD
 >
 > No, the problem is when you do use them, just not the ones you want.

Yeah.

For example, there is no way for the compiler to know what the type of 
x2 below is.

X foo(bool condition, int i, double d)
{
     X x = (condition ? i : d);    // X is either int or double
     bar(x);                       // Calls bar(int) or bar(double)
     return x;                     // Returns int or double
}

X x2 = foo(cond, 42, 1.5);        // How to generate code for x2?

D could do what dynamically typed languages do but then it would not be 
a statically typed language. :)

Ali



More information about the Digitalmars-d mailing list