| Thread overview | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 10, 2015 It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
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 | ||||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | You can do this with variants or even just plain strings. | |||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 10 June 2015 at 17:13:33 UTC, Adam D. Ruppe wrote:
> You can do this with variants or even just plain strings.
Hi, If it is not much trouble, care to explain how ?. I think that there would be need to have separate algorithms by data type. Maybe you can test the value of the data and make an algorithm that declares the variables accordingly but you the need to do this for all data.
BD
| |||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | 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
| |||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | 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
| |||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | 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.
Atila
| |||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Wednesday, 10 June 2015 at 17:19:30 UTC, Binarydepth wrote:
> Hi, If it is not much trouble, care to explain how ?
If it is user input, take it in the format they send it. Like:
string typed_data;
if(isNumeric(typed_data)) {
auto number = to!int(typed_data);
// do stuff with humber
} else {
// treat it as a string still
}
This tends to be the way I'd do it in python or ruby too.
| |||
June 10, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | 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
| |||
June 11, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | Here is a description of variants, with examples: http://dlang.org/phobos/std_variant.html | |||
June 11, 2015 Re: It may be useful to allow declaring variables without type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply