It may be useful to allow declaring variables without type

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 11 13:48:57 PDT 2015


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.

Please kill me now. Dynamic typing is almost always a _horrible_ 
idea. Sure, it may _look_ like it makes things easier up front, 
but it requires far more testing in order to ensure that you're 
getting it right (you're basically forced to have unit tests for 
all the sorts of things that a static type system automatically 
finds for you), and it's far less maintainable, because it's far 
easier to misunderstand what's going on and screw up the types 
when you're working on someone else's code or coming back to code 
that you haven't worked on in a while.

And honestly, the fact that you can do the equivalent of

if(condition)
     myVar = "foo";
else
     myVar = 42;

in languages like python just plain horrifies me. How on earth is 
that a _good_ idea? Sure, it can work if you're careful, and good 
code won't have if statements like this, but why not just use 
static typing and avoid all of the bugs and pain that allowing 
that sort of thing causes? I truly see nothing good about 
supporting dynamic types as the normal way of doing things.

And maybe I'm being too brusque with this post, but I truly do 
not understand how anyone can think that dynamic typing was ever 
a good idea outside of a few cases where you have no choice, and 
it's a big pet peeve of mine. Dynamic typing is just begging for 
problems and forces you to manually do checking that a static 
type system would have done for you on its own. It seems to me 
that using dynamic typing is like not having stoplights. Sure, it 
may _seem_ like you'll be able to get there faster, because you 
want have to stop at those pesky lights, but in reality, every 
intersection then becomes a potential disaster zone, since you no 
longer know that no one is going to enter the intersection from 
the other road, and so have to constantly drive very defensively 
and drive more slowly in order to avoid getting into an accident. 
Static typing is your friend. It finds bugs for you and prevents 
you from shooting yourself in the foot. In fact, many of C's 
safety problems come from allowing you to circumvent their static 
type system via casting.

That being said, there are rare cases where you really have no 
choice but to use dynamic typing - e.g. when dealing with 
database queries; the types are based on the database schema, and 
therefore can't be part of the API, since the API is not 
schema-specific. So, for those cases, we have std.variant.Variant 
and similar types in std.variant where they're helper structs 
around unions so that they're safer and cleaner to use by knowing 
about what type they currently hold (and in some cases, only 
allow certain types). So, you can have dynamic types when you 
need them, but they're not part of the language, and they 
shouldn't be used unless you actually need them.

Adam Ruppe did a talk on this topic at dconf, so you should check 
that out once the videos are up.

- Jonathan M Davis


More information about the Digitalmars-d mailing list