www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - test[0u] of type bool[1u] does not have a boolean value

reply "Namespace" <rswhite4 googlemail.com> writes:
Is this a bug or is it just me? It seems that the compiler 
dereference wrong.
----
import std.stdio;

void foo(bool[1]* test) {
	if (test[0])
		test[0] = false;
}

void main()
{
	bool[1] test = false;
	foo(&test);
}
----
prints: Error: expression test[0u] of type bool[1u] does not have 
a boolean value

This work:
----
if ((*test)[0])
     test[0] = false;
----
Jun 29 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 29 June 2013 at 12:41:12 UTC, Namespace wrote:
 Is this a bug or is it just me? It seems that the compiler 
 dereference wrong.
 ----
 import std.stdio;

 void foo(bool[1]* test) {
 	if (test[0])
 		test[0] = false;
 }

 void main()
 {
 	bool[1] test = false;
 	foo(&test);
 }
 ----
 prints: Error: expression test[0u] of type bool[1u] does not 
 have a boolean value

 This work:
 ----
 if ((*test)[0])
     test[0] = false;
 ----
bool[1]*: a pointer to a static array of bools of size 1. Ergo test[0] is of type "bool[1]". Which can't be evaluated to bool. When you write "test[0] = false", that is actually an *array assignement* (test[0] is the same as *test, which resolves to a bool[1]), and yo are assigning false to *all* (in this case, 1) elements of your array. On the other hand, (*test)[0] first dereferences the pointer to obtain the array, and then obtains the first element... The assignment on the next line is still wrong though. So I think it's just you ;) But in your defense, (I think you have a C++ background?) the declaration syntax from D to C++ is completely different... Related: I think this might actually give you a compiler warning about doing a range assign without slicing? Bearophile had suggested this shouldn't work unless you actually type: "test[0][] = false;" But I prefer: "test[0] []= false;" I can't test right now: Does your code emit no warnings with -w ?
Jun 29 2013
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
I get this with -wi:
bug.d(5): Warning: explicit element-wise assignment (test[0u])[] 
= false is bett
er than test[0u] = false

That helps a bit. But I thought that D dereferences 
automatically? ;)
Jun 29 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 29 June 2013 at 12:57:07 UTC, Namespace wrote:
 I get this with -wi:
 bug.d(5): Warning: explicit element-wise assignment 
 (test[0u])[] = false is bett
 er than test[0u] = false

 That helps a bit. But I thought that D dereferences 
 automatically? ;)
Only when making a function call (AFAIK), eg: "p.foo();" This also takes precedence over UFCS: ---- struct S { void foo(){writeln("val");} } void foo(S*){writeln("pointer");} //troll function trying to hijack p.foo() void main() { S* p = new S; p.foo(); } -------- val --------
Jun 29 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
monarch_dodra:

 Related: I think this might actually give you a compiler 
 warning about doing a range assign without slicing? Bearophile 
 had suggested this shouldn't work unless you actually type:
 "test[0][] = false;"
 But I prefer:
 "test[0] []= false;"

 I can't test right now: Does your code emit no warnings with -w 
 ?
I have also suggested to have the "-wi" switch activated on default (as I think C# does, because people forget to use it all the time!), give a switch to disable on request the informational warnings (because once in a while you don't want warnings), and remove the -w switch (because it breaks the semantics of D programs) :-) http://d.puremagic.com/issues/show_bug.cgi?id=10321 Bye, bearophile
Jun 29 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 29 June 2013 at 13:11:10 UTC, bearophile wrote:
 monarch_dodra:

 Related: I think this might actually give you a compiler 
 warning about doing a range assign without slicing? Bearophile 
 had suggested this shouldn't work unless you actually type:
 "test[0][] = false;"
 But I prefer:
 "test[0] []= false;"

 I can't test right now: Does your code emit no warnings with 
 -w ?
I have also suggested to have the "-wi" switch activated on default (as I think C# does, because people forget to use it all the time!), give a switch to disable on request the informational warnings (because once in a while you don't want warnings), and remove the -w switch (because it breaks the semantics of D programs) :-) http://d.puremagic.com/issues/show_bug.cgi?id=10321 Bye, bearophile
Thanks for the link. I actually wanted to state that I think you were one of the people who thought the above should be mandatory syntax? I do.
Jun 29 2013