Thread overview
_Symbols _with _leading _underscores
Dec 17, 2022
Paul
Dec 17, 2022
user1234
Dec 17, 2022
Adam D Ruppe
Dec 19, 2022
Paul
December 17, 2022

I see code like this from time to time. Are the leading underscores significant, in general, in the D language? Is it just programmer preference? Is it a coding practice, in general, that is common...even outside of D? Thanks for any assistance.

From: http://dpldocs.info/this-week-in-d/Blog.Posted_2022_10_10.html#hello-arduino

import ldc.llvmasm;

// Ports from the delay_basic.h in the thing
void _delay_loop_1(ubyte __count) {
	// this template param is required to avoid
	// assertion `!Call.getType()->isVoidTy() && "Bad inline asm!"' failed.

        __asm!ubyte (
                "1: dec $0\n\tbrne 1b",
                "=r,0", (__count)
        );
}
void _delay_loop_2(ushort __count) {
        __asm!ushort (`
	1: sbiw $0,1
	brne 1b
	`,
	`=w,0`,
	__count);
}

// port from delay.h in arduino thing
enum F_CPU = 1_000_000UL;

// this was _delay_ms but i did something wrong and changed double to int and i still don't love it
void _delay(int __ms) {
	ushort __ticks;
	ulong __tmp = (F_CPU * __ms) / 4000;
	if(__tmp < 1)
		__ticks = 1;
	else if(__tmp > 65_535) {
		__ticks = cast(ushort) (__ms * 10.0);
		while(__ticks) {
			_delay_loop_2(cast(ushort) (((F_CPU) / 4e3) / 10));
			__ticks--;
		}
		return;
	} else
		__ticks = cast(ushort) __tmp;
	_delay_loop_2(__ticks);
}
December 17, 2022

On Saturday, 17 December 2022 at 02:42:22 UTC, Paul wrote:

>

I see code like this from time to time. Are the leading underscores significant, in general, in the D language? Is it just programmer preference? Is it a coding practice, in general, that is common...even outside of D? Thanks for any assistance.

The only important thing you should know about is that doubly leading underscores are reserved for symbols generated by the D front-end, so you should avoid them. This is specified here: https://dlang.org/spec/lex.html#identifiers.

December 17, 2022
On Saturday, 17 December 2022 at 02:42:22 UTC, Paul wrote:
> Are the leading underscores significant, in general, in the D language?

The code you linked is a copy/paste from some C runtime library code, where the leading __ is the convention to indicate it is part of the private platform implementation, which user code shouldn't use. This ensures no name conflicts.

D has this rule too, you're actually not supposed to use __ prefixes in your own code either, since the compiler will use it for auto-generated names. But since it was a copy paste, I was too lazy to change it.

A single leading _ is sometimes used by some programmers to indicate private members, but that's no special meaning in the language.
December 19, 2022
Much appreciated...