digitalmars.D - It may be useful to allow declaring variables without type
- Binarydepth (6/6) Jun 10 2015 I think that it could be useful to declare variables as Python
- Adam D. Ruppe (1/1) Jun 10 2015 You can do this with variants or even just plain strings.
- Binarydepth (7/8) Jun 10 2015 Hi, If it is not much trouble, care to explain how ?. I think
- Adam D. Ruppe (10/11) Jun 10 2015 If it is user input, take it in the format they send it. Like:
- Atila Neves (6/12) Jun 10 2015 I disagree on the usefulness. I think this is one of the few
- Binarydepth (4/17) Jun 10 2015 Well, do think that can be solved by making the compiler report
- Atila Neves (4/24) Jun 10 2015 No, the problem is when you do use them, just not the ones you
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/37) Jun 10 2015 Yeah.
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (2/2) Jun 11 2015 Here is a description of variants, with examples:
- Jonathan M Davis (52/57) Jun 11 2015 Please kill me now. Dynamic typing is almost always a _horrible_
-
Laeeth Isharc
(16/18)
Jun 11 2015
- weaselcat (3/22) Jun 11 2015 python being an unmaintainable mess is the reason go even exists.
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
Jun 10 2015
You can do this with variants or even just plain strings.
Jun 10 2015
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
Jun 10 2015
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.
Jun 10 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. BDI 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
Jun 10 2015
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:Well, do think that can be solved by making the compiler report unused variables ? BDI 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. BDI 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
Jun 10 2015
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:No, the problem is when you do use them, just not the ones you want. AtilaOn Wednesday, 10 June 2015 at 17:04:20 UTC, Binarydepth wrote:Well, do think that can be solved by making the compiler report unused variables ? BDI 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. BDI 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
Jun 10 2015
On 06/10/2015 10:49 AM, Atila Neves wrote:On Wednesday, 10 June 2015 at 17:38:45 UTC, Binarydepth wrote: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. :) AliOn Wednesday, 10 June 2015 at 17:32:18 UTC, Atila Neves wrote:No, the problem is when you do use them, just not the ones you want.On Wednesday, 10 June 2015 at 17:04:20 UTC, Binarydepth wrote:Well, do think that can be solved by making the compiler report unused variables ? BDI 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. BDI 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
Jun 10 2015
Here is a description of variants, with examples: http://dlang.org/phobos/std_variant.html
Jun 11 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
Jun 11 2015
On Thursday, 11 June 2015 at 20:48:59 UTC, Jonathan M Davis wrote:Please kill me now. Dynamic typing is almost always a _horrible_ idea.<snip> I liked this post very much. I remember thinking how superior BBC Basic was to other languages for forcing you to declare variables. (I know the question is not technically only about this, but the philosophical tone is similar). And what a shock it was to travel back in time when I learnt python... https://en.wikipedia.org/wiki/BBC_BASIC Should this be turned into a wiki post? Maybe the main section relating to why D does things a certain way? Or maybe an opinionated section where there are articles that don't represent the official D stance. In general there is lots of hidden gold in newsgroup posts that is not always easy to find. Curating this might help others down the line. I can do a little, but not that much as I have certain constraints and other things I should be focusing on.
Jun 11 2015
On Thursday, 11 June 2015 at 21:48:33 UTC, Laeeth Isharc wrote:On Thursday, 11 June 2015 at 20:48:59 UTC, Jonathan M Davis wrote:python being an unmaintainable mess is the reason go even exists. dynamic typing is dead.Please kill me now. Dynamic typing is almost always a _horrible_ idea.<snip> I liked this post very much. I remember thinking how superior BBC Basic was to other languages for forcing you to declare variables. (I know the question is not technically only about this, but the philosophical tone is similar). And what a shock it was to travel back in time when I learnt python... https://en.wikipedia.org/wiki/BBC_BASIC Should this be turned into a wiki post? Maybe the main section relating to why D does things a certain way? Or maybe an opinionated section where there are articles that don't represent the official D stance. In general there is lots of hidden gold in newsgroup posts that is not always easy to find. Curating this might help others down the line. I can do a little, but not that much as I have certain constraints and other things I should be focusing on.
Jun 11 2015