Lars T. Kyllingstad:
> You don't have to write 'auto' to get type inference. You just have to
> have something that signifies to the compiler that a variable is
> declared. All of these are valid:
>
> auto x = 123; // x is int
> const y = 1.23; // y is const(double)
>
Joseph Wakeling wrote:
Also try:
foreach(uint i;0..1) {
scope Foo f = new Foo(i);
// Do something with f ...
}
That sometimes doesn't require allocations.
To be sure I understand -- is there anything wrong with writing
scope auto f = new Foo(i)
... or, in general, using auto
Joseph Wakeling:
> is there anything wrong with writing
> scope auto f = new Foo(i)
If the compiler is cool with that, then it's OK :-)
Bye,
bearophile
Hello Joseph,
To be sure I understand -- is there anything wrong with writing
scope auto f = new Foo(i)
BTW, the following might work:
scope f = new Foo(i);
--
... <
> Also try:
>
> foreach(uint i;0..1) {
>scope Foo f = new Foo(i);
>// Do something with f ...
> }
>
> That sometimes doesn't require allocations.
To be sure I understand -- is there anything wrong with writing
scope auto f = new Foo(i)
... or, in general, using auto to infer the
Joseph Wakeling:
> ... when it reappears in D as:
>
> foreach(uint i;0..1) {
> auto f = new Foo(i);
> // Do something with f ...
> }
Also try:
foreach(uint i;0..1) {
scope Foo f = new Foo(i);
// Do something with f ...
}
That sometimes doesn't require all
I wrote:
> I guess I am worried about what could happen in the case of code like this in
> C++:
>
> for(i=0;i<1;++i) {
> Foo f(i);
> // Do something with f ...
> }
>
> ... when it reappears in D as:
>
> foreach(uint i;0..1) {
> auto f = new Foo(i);
>
Thanks for the interesting and detailed explanation. :-)
> Now you see, 'new' is for dynamic memory allocation in D as well, it's
> just that for classes it is required. You normally don't need to worry
> about 'delete', as the GC will take care of deallocation.
I guess I am worried about what c
Joseph Wakeling wrote:
I also have some C++ experience, but it seems to be confusing as much as
complementary with respect to D ... :-)
Current source of confusion relates to declaring objects of a class whose
constructor takes input -- confusion because I can write,
class Foo
{