www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why are compile-time constraints checked with an (inout int = 0)

reply "Atila Neves" <atila.neves gmail.com> writes:
Why the (inout int = 0) instead of an empty parameter list? I
checkout how isInputRange was implemented and I copied the idiom,
but I'd like to know why it's like that instead of cargo culting.
Thanks,

Atila
Mar 26 2014
next sibling parent reply "Atila Neves" <atila.neves gmail.com> writes:
Seriously, nobody? I'd've thought this was quite important.

On Thursday, 27 March 2014 at 05:27:50 UTC, Atila Neves wrote:
 Why the (inout int = 0) instead of an empty parameter list? I
 checkout how isInputRange was implemented and I copied the 
 idiom,
 but I'd like to know why it's like that instead of cargo 
 culting.
 Thanks,

 Atila
Mar 28 2014
parent "Dicebot" <public dicebot.lv> writes:
On Friday, 28 March 2014 at 11:17:10 UTC, Atila Neves wrote:
 Seriously, nobody? I'd've thought this was quite important.

 On Thursday, 27 March 2014 at 05:27:50 UTC, Atila Neves wrote:
 Why the (inout int = 0) instead of an empty parameter list? I
 checkout how isInputRange was implemented and I copied the 
 idiom,
 but I'd like to know why it's like that instead of cargo 
 culting.
 Thanks,

 Atila
Friend of mine (learning D) has recently asked same question and I failed to answer :( I never use it in constraint checks (empty parameter list instead) and have not encountered any issue.
Mar 28 2014
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/27/14, Atila Neves <atila.neves gmail.com> wrote:
 Why the (inout int = 0) instead of an empty parameter list?
Try removing it and compile std.range with -unittest. Here's what happens: std\range.d(546): Error: static assert (isInputRange!(inout(int)[])) is false The reason it's false is because the code wouldn't compile. Here's a test-case: ----- import std.array; void test() { inout(int)[] r = (inout(int)[]).init; if (r.empty) {} r.popFront(); auto h = r.front; } void main() { test(); } ----- test.d(7): Error: variable test.test.r inout variables can only be declared inside inout functions So you need to add inout in the parameter list to avoid this compiler error and to make your function work with any qualified range type.
Mar 28 2014