Thread overview
dub -debug=tag for Windows
3 days ago
Brother Bill
3 days ago
0xEAB
3 days ago
0xEAB
3 days ago
0xEAB
3 days ago
Brother Bill
3 days ago

Page 462 of Programming in D book.

The goal is to only run debug(binarySearch), and not regular debug.
Looking for the CLI for dub or dmd.
Tried:

>

dmd source/app.d -w -debug=binarySearch
but it printed all the debug statements.

What CLI command will get the job done?

import std.stdio;

void main() {
    auto numbers = [-100, 0, 1, 2, 7, 10, 42, 365, 1000];
    auto index = binarySearch(numbers, 42);

    writeln("Index: ", index);
}

size_t binarySearch(const int[] values, int value) {
    debug writefln("searching %s among %s", value, values);

    if (values.length == 0) {
        debug(binarySearch) writefln("%s not found", value);
        return size_t.max;
    }

    immutable midPoint = values.length / 2;

    debug writefln("considering index %s", midPoint);

    if (value == values[midPoint]) {
        debug writefln("found %s at index %s", value, midPoint);
        return midPoint;

    } else if (value < values[midPoint]) {
        debug(binarySearch) writefln("must be in the first half");
        return binarySearch(values[0 .. midPoint], value);

    } else {
        debug writefln("must be in the second half");
        return binarySearch(values[midPoint + 1 .. $], value);
    }
}

3 days ago
On Sunday, 17 August 2025 at 12:39:59 UTC, Brother Bill wrote:
> The goal is to only run debug(binarySearch), and not regular debug.

How about giving the “regular” debug statement a named identifier as well?
Any `-debug` switch will enable the compilation of identifier-less debug statements.
3 days ago
On Sunday, 17 August 2025 at 14:13:48 UTC, 0xEAB wrote:
> Any `-debug` switch will enable the compilation of identifier-less debug statements.

Sorry, I was looking at the wrong output.
Unless you provide `-debug` as well, you should not get identifier-less ones compiled in as well.

3 days ago
On Sunday, 17 August 2025 at 14:15:37 UTC, 0xEAB wrote:
> Unless you provide `-debug` as well, you should not get identifier-less ones compiled in as well.

```
import std.stdio;

void main() {
    writeln("foo");
    debug writeln("bar");
    debug(foobar) writeln("foobar");
}
```

As far as I can tell, `dmd -debug=foobar -run app.d` reliably outputs across various compiler versions:

```
foo
foobar
```


3 days ago

On Sunday, 17 August 2025 at 14:17:42 UTC, 0xEAB wrote:

>

On Sunday, 17 August 2025 at 14:15:37 UTC, 0xEAB wrote:

>

Unless you provide -debug as well, you should not get identifier-less ones compiled in as well.

import std.stdio;

void main() {
    writeln("foo");
    debug writeln("bar");
    debug(foobar) writeln("foobar");
}

As far as I can tell, dmd -debug=foobar -run app.d reliably outputs across various compiler versions:

foo
foobar

Success!

> dmd -debug=binarySearch -run source/app.d
searching 42 among [-100, 0, 1, 2, 7, 10, 42, 365, 1000]
searching 42 among [10, 42, 365, 1000]
must be in the first half
searching 42 among [10, 42]
Index: 1
import std.stdio;

void main() {
    auto numbers = [-100, 0, 1, 2, 7, 10, 42, 365, 1000];
    auto index = binarySearch(numbers, 42);

    writeln("Index: ", index);
}

size_t binarySearch(const int[] values, int value) {
    debug(binarySearch) writefln("searching %s among %s", value, values);

    if (values.length == 0) {
        debug writefln("%s not found", value);
        return size_t.max;
    }

    immutable midPoint = values.length / 2;

    debug writefln("considering index %s", midPoint);

    if (value == values[midPoint]) {
        debug writefln("found %s at index %s", value, midPoint);
        return midPoint;

    } else if (value < values[midPoint]) {
        debug(binarySearch) writefln("must be in the first half");
        return binarySearch(values[0 .. midPoint], value);

    } else {
        debug writefln("must be in the second half");
        return binarySearch(values[midPoint + 1 .. $], value);
    }
}