| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
June 08, 2015 Template specializations in unittest blocks | ||||
|---|---|---|---|---|
| ||||
The following works:
enum foo(int x) = x + foo!(x - 1);
enum foo(int x : 0) = 0;
enum bar(T) = cast(T) 3;
enum bar(T : int) = 4;
Wrap it in a unittest{}, and complaints about multiply defined templates hit. Is there a reason for this?
| ||||
June 08, 2015 Re: Template specializations in unittest blocks | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jeffrey Tsang | On 06/08/2015 08:10 PM, Jeffrey Tsang wrote:
> The following works:
>
> enum foo(int x) = x + foo!(x - 1);
> enum foo(int x : 0) = 0;
>
> enum bar(T) = cast(T) 3;
> enum bar(T : int) = 4;
>
> Wrap it in a unittest{}, and complaints about multiply defined templates
> hit. Is there a reason for this?
No good reason. DMD doesn't let local symbols overload against each other.
| |||
June 08, 2015 Re: Template specializations in unittest blocks | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jeffrey Tsang | On 6/8/15 2:10 PM, Jeffrey Tsang wrote:
> The following works:
>
> enum foo(int x) = x + foo!(x - 1);
> enum foo(int x : 0) = 0;
>
> enum bar(T) = cast(T) 3;
> enum bar(T : int) = 4;
>
> Wrap it in a unittest{}, and complaints about multiply defined templates
> hit. Is there a reason for this?
To expand on what Timon said, a unittest block is actually a function under the hood.
Try the same thing inside a function and you will get the same issue.
Do this instead (outside of the unit test block):
version(unittest) {
... // same thing
}
-Steve
| |||
June 08, 2015 Re: Template specializations in unittest blocks | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 8 June 2015 at 18:22:49 UTC, Steven Schveighoffer wrote:
> On 6/8/15 2:10 PM, Jeffrey Tsang wrote:
>> The following works:
>>
>> enum foo(int x) = x + foo!(x - 1);
>> enum foo(int x : 0) = 0;
>>
>> enum bar(T) = cast(T) 3;
>> enum bar(T : int) = 4;
>>
>> Wrap it in a unittest{}, and complaints about multiply defined templates
>> hit. Is there a reason for this?
>
> To expand on what Timon said, a unittest block is actually a function under the hood.
>
> Try the same thing inside a function and you will get the same issue.
>
> Do this instead (outside of the unit test block):
>
> version(unittest) {
> ... // same thing
> }
>
> -Steve
OK, that makes much more sense. Thanks.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply