www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - do I incur a penality on compile time if I explicitly declare default

reply someone <someone somewhere.com> writes:
I mean, coding as following:

```d
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 ?
Jun 20
next sibling parent reply frame <frame86 live.com> writes:
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.
Jun 21
next sibling parent someone <someone somewhere.com> writes:
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.
Jun 21
prev sibling parent someone <someone somewhere.com> writes:
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 :)
Jun 21
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/21/21 12:12 AM, someone wrote:
 I mean, coding as following:
 
 ```d
 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
Jun 21
parent reply someone <someone somewhere.com> writes:
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: ```d 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
Jun 21
parent frame <frame86 live.com> writes:
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.
Jun 21
prev sibling parent max haughton <maxhaton gmail.com> writes:
On Monday, 21 June 2021 at 04:12:55 UTC, someone wrote:
 I mean, coding as following:

 ```d
 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.
Jun 22