Re: Unused imports

2020-01-30 Thread Francesco Mecca via Digitalmars-d-learn

On Thursday, 30 January 2020 at 16:23:54 UTC, Michael wrote:

Is there a way to detect unused imports?

It happened to me that I used imports which I did not need in 
the end. So, I'd like to remove them easily.


https://issues.dlang.org/show_bug.cgi?id=20442

TL;DR
This has come up in the past and the typical answer was: "This 
should be implemented as a third party tool using dmd as a 
library".


It is a problem that there are no currently available tools to do 
that (maybe visual-d greys out unused imports) but I do agree 
that it should be provided as a third party.


Such a functionality could end up in:
https://github.com/dlang-community/D-Scanner

and it is also a good first time project for a beginner in the D 
ecosystem.


"if" statement

2019-03-24 Thread Francesco Mecca via Digitalmars-d-learn

https://run.dlang.io/is/zRcj59

```
alias Alg = Algebraic!(int, string);

void main()
{
int n = 2;
Alg value;

value = n == 2 ? 2 : "string";
}
```

The original code used SumType but the effect is the same.

I suppose that I could write the following:

```
if(n == 2) value = 2;
else value = "string";
```

Is there a workaround for this that maintains a similar syntactic 
structure?
is this behaviour accepted or should the compiler translate the 
first case in the second?


Re: Map of functions

2018-12-14 Thread Francesco Mecca via Digitalmars-d-learn

On Friday, 14 December 2018 at 17:45:26 UTC, rjframe wrote:

On Fri, 14 Dec 2018 16:33:44 +, Giovanni Di Maria wrote:



I need the flow of calls.
Thank you Giovanni


gprof will do this on Linux/BSD if gdc supports the -pg flag (I 
don't know whether it would, but assume so) and your 
application is working.



From code, you'd need to call a trace function on function 
entry. Mine lets you also specify a message (parameter values, 
etc.):


---
void trace(T...)(T args, string func = __FUNCTION__) {
import std.stdio : writeln;
if (args.length > 0) {
debug writeln("*trace: ", func, "- ", args);
} else {
debug writeln("*trace: ", func);
}
}

void main(int a, string b) {
trace();
trace(a, ", ", b);
}
---


You can also generate a call graph using callgrind.
That however will slow down your program.

There are some python script that converts the output of 
callgrind to a dot file that can be converted to svg.


https://stackoverflow.com/questions/33769323/make-callgrind-show-all-function-calls-in-the-kcachegrind-callgraph