Getting __COLUMN__ of source code location.

realhet real_het at hotmail.com
Sat Jul 29 07:50:38 UTC 2023


On Thursday, 27 July 2023 at 16:17:28 UTC, IchorDev wrote:
> I'm not aware of any way to do that exact thing. Measuring what 
> column a line is on would be quite subjective.

When I compile(LDC2) a something with an error and using the 
--vcolumns argument I get this:
onlineapp.d(14,5): Error: found `}` when expecting `;` following 
statement
The error is on Line 14, Column 5.

(I don't care how much it counts the TAB character, as long as it 
is unique.)

So the information is accessible in the compiler.

I just can't access from the language.
```
I can access __LINE__ but I can't access something like __COL__.
```
I just hope there is other ways to get this, maybe with an LDC 
specific traits or something.

> But you'd have to provide a unique ID manually anyway if, for 
> instance, you create UI elements in a for loop:
> ```d
> for(size_t i=0; i<10; i++){
> 	button();
> }
> ```
>
> I'd suggest using something more like Dear ImGui's ID system 
> instead.

Thanks for suggesting I've checked.
As I see it is using an ID stack.
I do something similar with a 32bit hash and using XOR operation 
to simulate a stack. Also for debugging and critical stuff there 
is an option to store this id stack in a string with meaningful 
names.

So I can modify the id directly, when I pit the button() in a for 
loop.

All the Controls automatically modifying the id by their type, 
source module and source line number (just a hash or a meaningful 
string for debug).

```
line 15:    with(player){ if(Btn("Play")) play; if(Btn("Stop")) 
stop; }
```

So the 2 id's for the buttons will be the same: [Btn,module,15] 
and [Btn,module,15]   <- that's a duplicated id.

But if I'd have access to sourcecolumn, that would be so cool:
[Btn,module,15,22] and [Btn,module,15,34]


Here are the 2 workarounds right now:
```
line 15:    with(player){ if(Btn("Play"), genericId(1)) play; 
if(Btn("Stop"), genericId(1)) stop; }
```

```
line 15:    with(player){ if(Btn("Play")) play;
line 16:                  if(Btn("Stop")) stop; }
```


It's not a critically important feature, it's just helping the UI 
and the code that generates the UI being in the same visual 
layout: horizontal in this case.



More information about the Digitalmars-d-learn mailing list