Re: How to get the location (file, line) of UDA without parens?

2021-11-13 Thread user1234 via Digitalmars-d-learn
On Sunday, 14 November 2021 at 05:12:58 UTC, Andrey Zherikov 
wrote:

Here is my code:
[...]
`W()` (2) works as expected but is it possible to achieve the 
same without parenthesis so `getAttributes` trait returns 
`tuple(L("app.d", 16LU))` for (1)?


No, without parens this is really the function template, as a 
symbol, that becomes the UDA.


How to get the location (file, line) of UDA without parens?

2021-11-13 Thread Andrey Zherikov via Digitalmars-d-learn

Here is my code:
```d
struct L
{
string file;
size_t line;
}

auto W(string f = __FILE__,size_t l= __LINE__)()
{
return L(f,l);
}

struct A
{
  @W   // (1): line# 16
  {
int a;
int b;
  }
  @W() // (2): line# 21
  {
int c;
int d;
  }
}

void main()
{
pragma(msg, __traits(getAttributes, A.a)); // tuple(W(string 
f = __FILE__, ulong l = __LINE__)())
pragma(msg, __traits(getAttributes, A.b)); // tuple(W(string 
f = __FILE__, ulong l = __LINE__)())
pragma(msg, __traits(getAttributes, A.c)); // 
tuple(L("app.d", 21LU))
pragma(msg, __traits(getAttributes, A.d)); // 
tuple(L("app.d", 21LU))

}
```

`W()` (2) works as expected but is it possible to achieve the 
same without parenthesis so `getAttributes` trait returns 
`tuple(L("app.d", 16LU))` for (1)?


Re: using __traits to get line number of a member

2021-11-13 Thread forkit via Digitalmars-d-learn
On Sunday, 14 November 2021 at 04:24:09 UTC, Stanislav Blinov 
wrote:

On Sunday, 14 November 2021 at 04:05:45 UTC, forkit wrote:


However, there is no isClass method. Why not?

How do I determine if a member is a class.. I wonder...


```
static if (is(something == class)) { /* ... */ }
```

or, if member is an instance

```
static if (is(typeof(something) == class)) { /* ... */ }
```

Ditto for interfaces, structs, unions.


thanks :-)

works now...

if((is(mixin(member) == class)))
   writeln("This is a class.");



Re: Completing C code with D style

2021-11-13 Thread forkit via Digitalmars-d-learn

On Saturday, 13 November 2021 at 23:02:15 UTC, pascal111 wrote:


I touch that D is big language, it's not small like standard C. 
This will cost me much studying, so I think I need slow down 
and learn it step by step.


Yes. C is so much smaller, and thus simpler (till you wanna do 
something complex)


But C also makes it 'simpler' to shoot yourself in the foot ;-)

Even with your simple C code, which is so easily tranposed to D, 
you already benefit from:


- variables being always initialised before use
(i.e no need to initalise i in your for loop, in D)

- array indices being automatically checked for out-of-bounds
(i.e. i < 10  ... in D.. it becomes.. numbers.length )

so you can make your simple code, even 'simpler' in D ;-)



Re: using __traits to get line number of a member

2021-11-13 Thread Stanislav Blinov via Digitalmars-d-learn

On Sunday, 14 November 2021 at 04:05:45 UTC, forkit wrote:


However, there is no isClass method. Why not?

How do I determine if a member is a class.. I wonder...


```
static if (is(something == class)) { /* ... */ }
```

or, if member is an instance

```
static if (is(typeof(something) == class)) { /* ... */ }
```

Ditto for interfaces, structs, unions.


Re: using __traits to get line number of a member

2021-11-13 Thread forkit via Digitalmars-d-learn
On Saturday, 13 November 2021 at 17:22:16 UTC, Stanislav Blinov 
wrote:

On Saturday, 13 November 2021 at 08:04:56 UTC, forkit wrote:


int i;
foreach(m; __traits(allMembers, mixin(__MODULE__)))
// ...
__traits(getLocation, mixin(m))[1]);


What you really should be doing is this:

```d
static import mod = mixin(__MODULE__);
foreach (i, name; __traits(allMembers, mod))
{
   // ...
   __traits(getLocation, __traits(getMember, mod, i));
   // ...
}
```

Otherwise you might run into name conflicts, and get location 
of a wrong symbol.


Also if you really want to be generic you should couple it with 
`__traits(getOverloads)`, like the docs for getLocation suggest.


static import mod = mixin(__MODULE__);

That statement above would not compile. After spending way too 
much time trying to work out, I gave up, and used what i have, 
which works.


I just want to know, which members in my module are functions, 
and which are classes. I got the isFunction  (that method exists)


However, there is no isClass method. Why not?

How do I determine if a member is a class.. I wonder...

e.g.

//=
module test;

import std;

class myClass{} // nothing to see here

void main()
{
alias allMembersOfThisModule = __traits(allMembers, 
mixin(__MODULE__));
string memberFile() { return `__traits(getLocation, 
mixin(member))[0]`; }
string memberLocation() { return `__traits(getLocation, 
mixin(member))[1]`; }


foreach(member; allMembersOfThisModule)
{
static if(member == "std" || member == "object")
{
continue;
}
else
{
writefln("\nHere is a member: %s", member);

// what kind of member is it?
if(isFunction!(mixin(member)))
writeln("This is a function.");
//else
//if(isClass!(mixin(member)))
   //writeln("This is a class.");
else
writeln("Not really sure what type of member this 
is..");


writefln("%s is located in file: %s", member, 
mixin(memberFile));
writefln("%s begins on line number: %s", member, 
mixin(memberLocation));

}
}
}

void myFunction(){} // nothing to see here

// =


Re: D modules

2021-11-13 Thread Adam Ruppe via Digitalmars-d-learn

On Saturday, 13 November 2021 at 22:52:55 UTC, pascal111 wrote:
When I'm searching for "toUpper" and "toLower" functions that 
string type uses


They are usable though `import std.string;` the docs just don't 
do a great job showing that.


The newest test version of my doc generator does integrate them 
into the member list though:


http://dpldocs.info/experimental-docs/std.string.html

My dpldocs.info website has a search engine too to help find the 
original place:


dpldocs.info/toLower

for example will point you at std.uni first.

But if it is "public imported" into a member list of another 
module you can use it just importing either way.


Re: D modules

2021-11-13 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 13 November 2021 at 22:52:55 UTC, pascal111 wrote:
When I'm searching for "toUpper" and "toLower" functions that 
string type uses, I confused when I reached the module 
"std.string". In the first section of its page 
"https://dlang.org/phobos/std_string.html; I didn't found 
functions I'm searching for, but when I pressed ctrl+f and 
typed function names, I found 'em in another section of the 
page, but I didn't understand if I have to add something to 
module name "std.string" or not, and I don't know how modules 
divided nor its sections way in D. I hope someone clarifies 
this obscure point to me and how can I search correctly for 
sought functions.


Those functions used to be in `std.string` a long time ago and 
are no more. As did some others. The section where you found them 
in the `std.string` docs is a table listing their current 
location in `std.uni` and links to their documentation. It also 
says this just above the table:



The following functions are publicly imported:


This means if you are already importing `std.string`, you do not 
need to import `std.uni` for `toUpper` and `toLower`, as 
`std.string` publicly imports them, so they will be available for 
you to use. So you can use those functions by importing 
`std.string` or `std.uni`.


https://dlang.org/phobos/std_uni.html#.toLower
https://dlang.org/phobos/std_uni.html#.toUpper


Re: Completing C code with D style

2021-11-13 Thread pascal111 via Digitalmars-d-learn

On Thursday, 11 November 2021 at 22:30:12 UTC, forkit wrote:

On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote:

[...]


ok.. for a more on topic response..

First: Please name your variables sensibly:
 char negativity, even; // grrr!!!
 char answer1, answer2; // makes so much more sense

[...]


I touch that D is big language, it's not small like standard C. 
This will cost me much studying, so I think I need slow down and 
learn it step by step.


Re: D modules

2021-11-13 Thread Imperatorn via Digitalmars-d-learn

On Saturday, 13 November 2021 at 22:52:55 UTC, pascal111 wrote:
When I'm searching for "toUpper" and "toLower" functions that 
string type uses, I confused when I reached the module 
"std.string". In the first section of its page 
"https://dlang.org/phobos/std_string.html; I didn't found 
functions I'm searching for, but when I pressed ctrl+f and 
typed function names, I found 'em in another section of the 
page, but I didn't understand if I have to add something to 
module name "std.string" or not, and I don't know how modules 
divided nor its sections way in D. I hope someone clarifies 
this obscure point to me and how can I search correctly for 
sought functions.


They are publicly imported from std.uni


D modules

2021-11-13 Thread pascal111 via Digitalmars-d-learn
When I'm searching for "toUpper" and "toLower" functions that 
string type uses, I confused when I reached the module 
"std.string". In the first section of its page 
"https://dlang.org/phobos/std_string.html; I didn't found 
functions I'm searching for, but when I pressed ctrl+f and typed 
function names, I found 'em in another section of the page, but I 
didn't understand if I have to add something to module name 
"std.string" or not, and I don't know how modules divided nor its 
sections way in D. I hope someone clarifies this obscure point to 
me and how can I search correctly for sought functions.


Re: using __traits to get line number of a member

2021-11-13 Thread Stanislav Blinov via Digitalmars-d-learn

On Saturday, 13 November 2021 at 08:04:56 UTC, forkit wrote:


int i;
foreach(m; __traits(allMembers, mixin(__MODULE__)))
// ...
__traits(getLocation, mixin(m))[1]);


What you really should be doing is this:

```d
static import mod = mixin(__MODULE__);
foreach (i, name; __traits(allMembers, mod))
{
   // ...
   __traits(getLocation, __traits(getMember, mod, i));
   // ...
}
```

Otherwise you might run into name conflicts, and get location of 
a wrong symbol.


Also if you really want to be generic you should couple it with 
`__traits(getOverloads)`, like the docs for getLocation suggest.


Re: Is DMD still not inlining "inline asm"?

2021-11-13 Thread Basile B. via Digitalmars-d-learn

On Friday, 12 November 2021 at 00:46:05 UTC, Elronnd wrote:

On Thursday, 11 November 2021 at 13:22:15 UTC, Basile B. wrote:

As for now, I know no compiler that can do that.


GCC can do it.  Somewhat notoriously,


you meant "infamously" ?

LTO can lead to bugs from underspecified asm constraints 
following cross-TU inlining.


I have missed the LTO train TBH, gotta try that once...


Re: using __traits to get line number of a member

2021-11-13 Thread forkit via Digitalmars-d-learn

On Saturday, 13 November 2021 at 07:20:14 UTC, Ali Çehreli wrote:


It works because we mix-in the value of the string 'm', which 
becomes a symbol.


('foreach' instead of 'static foreach' works as well.)

Ali


Thanks. Really appreciate the help provided in this thread :-)

Final working code below:

//===

module test;

import std;

class myClass{ void foo(){}}

void myFunction1(){}

void main()
{
// list the first user defined member of this module (other 
than main)


int i;
foreach(m; __traits(allMembers, mixin(__MODULE__)))
{
// ignore these members
static if(m == "std" || m == "object" || m == "main" || m 
== "_d_run_main" || m == "_Dmain"  )

{
   i = 1;
   continue;
}
else
{
writefln("The name of the first user member is: %s", 
m);


// getLocation returns a tuple. See note below
writefln("%s begins on line number: %s", m, 
__traits(getLocation, mixin(m))[1]);

}

if(i == 1) break;
}
}


void myFunction2(){}


/*
https://dlang.org/spec/traits.html#getLocation

get the location of a symbol where the argument was declared.

getLocation Returns a tuple(string, int, int) whose entries 
correspond to

[0] string -> the filename
[1] int -> line number
[2] int -> column number
*/

// ==