Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 13, 2014 Untyped string variable fails silently. No compiler warning given. | ||||
---|---|---|---|---|
| ||||
Rather nasty bug that silently fails. Linux DMD64 D Compiler v2.065 import std.stdio; import std.array; import std.conv; import std.digest.crc; import std.random; class Foo { public string generateHash() { // The following variable is untyped // yet no compiler error is thrown. text = Random(unpredictableSeed).front.to!(string); // text is null here. return hexDigest!(CRC32)(text).array.to!(string); } } void main(string[] args) { auto foo = new Foo(); writefln("%s", foo.generateHash()); } Reported: https://d.puremagic.com/issues/show_bug.cgi?id=12357 |
March 13, 2014 Re: Untyped string variable fails silently. No compiler warning given. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Thursday, 13 March 2014 at 13:57:49 UTC, Gary Willoughby wrote: > text = Random(unpredictableSeed).front.to!(string); This is actually calling the function std.conv.text.... this line is rewritten to: std.conv.text(Random(unpredictableSeed).front.to!(string)); The text function converts all is arguments to string and returns them all concated together. It thinks you are trying to use a property setter. > // text is null here. > return hexDigest!(CRC32)(text).array.to!(string); And here, it is calling the text function with no arguments and no parens (similarly to a property getter). With no arguments, it just returns null. So this isn't actually a bug, D is working as designed, but it is a better example than the common "writeln = 1;" thing given in most property setter arguments... |
March 13, 2014 Re: Untyped string variable fails silently. No compiler warning given. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On 3/13/14, Gary Willoughby <dev@nomad.so> wrote:
> Rather nasty bug that silently fails. Linux DMD64 D Compiler v2.065
Unfortunately it's not a variable, it's the std.conv.text function being called. I've actually ran into this nasty "bug" once already.
It all boils down to being allowed to call functions via "foo = bar;"
-----
void text(string x) { }
void main()
{
text = "";
}
-----
However I think this will be finally fixed once property enforcement is properly implemented.
|
March 13, 2014 Re: Untyped string variable fails silently. No compiler warning given. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Thursday, 13 March 2014 at 14:07:22 UTC, Adam D. Ruppe wrote:
> On Thursday, 13 March 2014 at 13:57:49 UTC, Gary Willoughby wrote:
>> text = Random(unpredictableSeed).front.to!(string);
>
> This is actually calling the function std.conv.text.... this line is rewritten to:
>
> std.conv.text(Random(unpredictableSeed).front.to!(string));
>
> The text function converts all is arguments to string and returns them all concated together.
>
> It thinks you are trying to use a property setter.
>
>> // text is null here.
>> return hexDigest!(CRC32)(text).array.to!(string);
>
> And here, it is calling the text function with no arguments and no parens (similarly to a property getter). With no arguments, it just returns null.
>
>
>
>
> So this isn't actually a bug, D is working as designed, but it is a better example than the common "writeln = 1;" thing given in most property setter arguments...
Ah yes of course!
|
Copyright © 1999-2021 by the D Language Foundation