digitalmars.D - Is it intentional that forward references in unittests aren't
- Trass3r <un known.com> Jun 03 2010
- Ellery Newcomer <ellery-newcomer utulsa.edu> Jun 03 2010
- bearophile <bearophileHUGS lycos.com> Jun 03 2010
- bearophile <bearophileHUGS lycos.com> Jun 03 2010
- retard <re tard.com.invalid> Jun 03 2010
- "Steven Schveighoffer" <schveiguy yahoo.com> Jun 04 2010
- "Steven Schveighoffer" <schveiguy yahoo.com> Jun 04 2010
void main()
{
}
unittest
{
struct S
{
S2 s;
}
struct S2
{
}
S s;
}
yields:
unittests.d(9): Error: identifier 'S2' is not defined
unittests.d(9): Error: S2 is used as a type
unittests.d(8): Error: no size for type _error_
Error: no size for type _error_
while putting S2 in front of S works makes it work.
Is this intentional?
Jun 03 2010
On 06/03/2010 05:04 PM, Trass3r wrote:void main() { } unittest { struct S { S2 s; } struct S2 { } S s; } yields: unittests.d(9): Error: identifier 'S2' is not defined unittests.d(9): Error: S2 is used as a type unittests.d(8): Error: no size for type _error_ Error: no size for type _error_ while putting S2 in front of S works makes it work. Is this intentional?
It's intentional. Forward references generally aren't allowed inside function bodies.
Jun 03 2010
Trass3r:void main() { } unittest { struct S { S2 s; }
Being unittests functions, it can be better to use "static struct" there instead of "struct". ------------------------- Ellery Newcomer:It's intentional. Forward references generally aren't allowed inside function bodies.
Unittests being normal functions is an abstraction that leaks a bit, but I presume it's OK. Bye, bearophile
Jun 03 2010
retard:What does this mean? Can't you write a wrapper struct/class inside the block if you don't want to expose S and S2 outside the unittest block.
Seen from a high level a unittest section != function. But in D they are implemented as a kind-of-function anyway. So unittests in D2 can show some unwanted characteristics that are present in functions. This means that they are an abstraction that "leaks" (a bit): http://en.wikipedia.org/wiki/Leaky_abstraction But I have not criticized this design choice because overall the alternatives can be worse. Bye, bearophile
Jun 03 2010
Thu, 03 Jun 2010 18:30:04 -0400, bearophile wrote:Trass3r:void main() { } unittest { struct S { S2 s; }
Being unittests functions, it can be better to use "static struct" there instead of "struct". ------------------------- Ellery Newcomer:It's intentional. Forward references generally aren't allowed inside function bodies.
Unittests being normal functions is an abstraction that leaks a bit, but I presume it's OK.
What does this mean? Can't you write a wrapper struct/class inside the block if you don't want to expose S and S2 outside the unittest block.
Jun 03 2010
On Thu, 03 Jun 2010 18:04:26 -0400, Trass3r <un known.com> wrote:void main() { } unittest { struct S { S2 s; } struct S2 { } S s; } yields: unittests.d(9): Error: identifier 'S2' is not defined unittests.d(9): Error: S2 is used as a type unittests.d(8): Error: no size for type _error_ Error: no size for type _error_ while putting S2 in front of S works makes it work. Is this intentional?
unittest is a function. It's basically a single function per module that is a concatenation of all the unit tests in the module. Inside functions, forward references are not allowed. You can declare types or include imports using version(unittest) and then forward references should work. -Steve
Jun 04 2010
On Thu, 03 Jun 2010 20:44:00 -0400, retard <re tard.com.invalid> wrote:Thu, 03 Jun 2010 18:30:04 -0400, bearophile wrote:Trass3r:void main() { } unittest { struct S { S2 s; }
Being unittests functions, it can be better to use "static struct" there instead of "struct". ------------------------- Ellery Newcomer:It's intentional. Forward references generally aren't allowed inside function bodies.
Unittests being normal functions is an abstraction that leaks a bit, but I presume it's OK.
What does this mean? Can't you write a wrapper struct/class inside the block if you don't want to expose S and S2 outside the unittest block.
When compiling unittests, the version 'unittest' is defined, so something like this should work: version(unittest) { struct S { S2 s; } struct S2 {} } -Steve
Jun 04 2010









bearophile <bearophileHUGS lycos.com> 