I'd love to see DScript one day ...
Chris via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jun 15 04:30:45 PDT 2016
On Wednesday, 15 June 2016 at 10:08:41 UTC, Ola Fosheim Grøstad
wrote:
>
> That's just because your example isn't realistic. A realistic
> case in Python etc is where you accidentally assign when you
> wanted to introduce a new symbol. That is not a typing issue.
>
> A realistic D/C++ scenario:
>
> import std.stdio;
> // imported from libraries:
>
> auto create_name(string n){
> return ["PersonName",n];
> }
>
> auto create_name(const(char)* n){
> import core.stdc.string: strlen;
> auto slice = n[0 .. strlen(n)];
> return slice.dup;
> }
>
>
> void main(){
> auto myname = create_name("Ola");
> writeln("Letter count: ", myname.length);
> }
It's much harder to shoot yourself in the foot, though:
`
auto create_name(string n)
{
return ["PersonName",n];
}
auto create_name(const(char)* n)
{
import core.stdc.string: strlen;
auto slice = n[0 .. strlen(n)];
return slice.dup;
}
void main()
{
auto myname = create_name("Ola");
writeln("Letter count: ", myname.length);
auto p = Person();
p.firstName = myname;
writeln(p.firstName);
}
struct Person
{
char[] firstName;
char[] secondName;
}
`
Error: cannot implicitly convert expression (myname) of type
string[] to char[]
If you have
`
void main()
{
import std.conv : to;
auto myname = create_name("Ola");
writeln("Letter count: ", myname.length);
auto p = Person();
p.firstName = to!string(myname);
writeln(p.firstName);
}
struct Person
{
string firstName;
string secondName;
}
`
Then you will convert `string[]` into the string `["PersonName",
"Ola"]`, and you have a bug. However, factually, I hardly ever
encounter bugs like this, whereas in Python this happens quite a
lot once you deal with more than one module.
More information about the Digitalmars-d
mailing list