Thread overview
do I incur a penality on compile time if I explicitly declare default behavior ?
Jun 21, 2021
someone
Jun 21, 2021
frame
Jun 21, 2021
someone
Jun 22, 2021
someone
Jun 21, 2021
someone
Jun 22, 2021
frame
Jun 22, 2021
max haughton
June 21, 2021

I mean, coding as following:

int intWhatever = 0; /// default being zero anyway

foreach (classComputer objComputer, objComputers) { ... } /// explicitly declaring the type instead of letting the compiler to figure it out

struc Whatever {

   public doSomething() { ... } /// explicitly declaring scopes matching the default ones

}

string[] strWhatever;
if (strWhatever.length > cast(size_t) 1) { ... } /// explicitly casting for proper types although not required to at all

... and the likes; or, besides unnecessary typing, are there any cons that I should be aware of while using DMD ?

June 21, 2021

On Monday, 21 June 2021 at 04:12:55 UTC, someone wrote:

>

I mean, coding as following:

Even if it would have an impact - it may change with a new compiler release. I personally use explicit declaration in a foreach loop, because the IDEs don't get the type and it cost me more time to figure out the method signature on objects by this item than compiling.

June 21, 2021

On 6/21/21 12:12 AM, someone wrote:

>

I mean, coding as following:

int intWhatever = 0; /// default being zero anyway

foreach (classComputer objComputer, objComputers) { ... } /// explicitly declaring the type instead of letting the compiler to figure it out

struc Whatever {

    public doSomething() { ... } /// explicitly declaring scopes matching the default ones

}

string[] strWhatever;
if (strWhatever.length > cast(size_t) 1) { ... } /// explicitly casting for proper types although not required to at all

... and the likes; or, besides unnecessary typing, are there any cons that I should be aware of while using DMD ?

For sure there is a difference in what the compiler has to do.

But I think the check is likely trivial, and inconsequential as far as compiler runtimes. D is going to already figure out the types of expressions with or without explicit types. The extra step is when it has to check if it can convert from the detected type to the declared one.

I would be more concerned with possible conversions you didn't expect being silently performed by the compiler. e.g. if a type is alias this'd to a string, and you declare string as the foreach type, then it's going to run the alias this at runtime, even if that might be expensive.

This might happen even though you wrote the actual type at the time -- sometimes library code changes the type, and just uses alias this to allow original code to compile.

-Steve

June 21, 2021

On Monday, 21 June 2021 at 08:06:06 UTC, frame wrote:

>

Even if it would have an impact - it may change with a new compiler release. I personally use explicit declaration in a foreach loop, because the IDEs don't get the type and it cost me more time to figure out the method signature on objects by this item than compiling.

I am used to explicitly declare everything on production-level code, since I know beforehand what I am doing / trying to do and I am not inclined to let the compiler figure it out for me ... however, scripting/prototyping/demoing is totally a different matter, so be able to carry-on without explicit declarations is a plus for the language. Furthermore, I would love to have a switch on DMD to warn me/forbid me to write anything not explicitly declared/initialized.

June 21, 2021

On Monday, 21 June 2021 at 13:23:04 UTC, Steven Schveighoffer wrote:

>

For sure there is a difference in what the compiler has to do.

Indeed.

>

But I think the check is likely trivial, and inconsequential as far as compiler runtimes. D is going to already figure out the types of expressions with or without explicit types. The extra step is when it has to check if it can convert from the detected type to the declared one.

Indeed to. I think that when I asked for advice I was not thinking very much on compile times but far more on adding unneeded complexity or, far worse, shooting me in the foot due to sheer ignorance, so, better ask beforehand.

>

I would be more concerned with possible conversions you didn't expect being silently performed by the compiler. e.g. if a type is alias this'd to a string, and you declare string as the foreach type, then it's going to run the alias this at runtime, even if that might be expensive.

On this I am fully-covered since I always convert with cast() even for stupid cases like:

long intWhatever = 0L;

I never, ever, get compilation warnings or bugs for this type of stuff :) It is hard-wired on me.

>

This might happen even though you wrote the actual type at the time -- sometimes library code changes the type, and just uses alias this to allow original code to compile.

For what I was reading a couple of days ago while navigating the general forum, alias is something very useful that should be handled with care.

>

-Steve

June 22, 2021

On Monday, 21 June 2021 at 08:06:06 UTC, frame wrote:

>

Even if it would have an impact ...

Furthermore, regardless of the impact, one of the pros of explicitly coding like this is to help future-portings of the base code to another language if need so, the team porting the code won't be required to be knee-deep on the language whereabouts of the code being ported because the obvious is already stated in the code itself. The team won't need to know beforehand that in D everything inside a structure/class is public by default for variables declared without scope modifiers etc etc.

But, as a footnote, of course I am thinking the other way around: just tinkering with the idea of porting things into D and not away from D :)

June 22, 2021

On Monday, 21 June 2021 at 22:56:30 UTC, someone wrote:

> >

This might happen even though you wrote the actual type at the time -- sometimes library code changes the type, and just uses alias this to allow original code to compile.

For what I was reading a couple of days ago while navigating the general forum, alias is something very useful that should be handled with care.

>

-Steve

Oh yeah, 'alias this' can be problematic - first time I used it was fine. Later with more complex code, the compiler did run into some recursion by parsing the code, was not able to detect that and just assigned the type to void and has thrown funny errors.

>

Furthermore, regardless of the impact, one of the pros of explicitly coding like this is to help future-portings of the base code to another language

Well, your topic was about compile time ;)
Btw, some IDE can show you module import times like Visual Studio Code with d-code extension.

June 22, 2021

On Monday, 21 June 2021 at 04:12:55 UTC, someone wrote:

>

I mean, coding as following:

int intWhatever = 0; /// default being zero anyway

foreach (classComputer objComputer, objComputers) { ... } /// explicitly declaring the type instead of letting the compiler to figure it out

struc Whatever {

   public doSomething() { ... } /// explicitly declaring scopes matching the default ones

}

string[] strWhatever;
if (strWhatever.length > cast(size_t) 1) { ... } /// explicitly casting for proper types although not required to at all

... and the likes; or, besides unnecessary typing, are there any cons that I should be aware of while using DMD ?

Short answer: No

Longer answer: Still no but it does exercise different code in the compiler which you could measure if you were mad.