| Thread overview | |||||||||
|---|---|---|---|---|---|---|---|---|---|
|
March 29, 2008 Small feature request | ||||
|---|---|---|---|---|
| ||||
Look at this class.
class Buffer
{
private int bufferSize = 4096;
private void[bufferSize.init] buffer = void;
}
What's the capacity of buffer upon object construction?
I expect that buffer.length == 4096, since bufferSize == 4096, but get buffer.length == 0, since bufferSize.init == int.init.
It works not as expected and confuses a little. Do I have any chanse this will be fixed anytime, or is it intended?
| ||||
March 29, 2008 Re: Small feature request | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Koroskin Denis Wrote:
> Look at this class.
>
> class Buffer
> {
> private int bufferSize = 4096;
> private void[bufferSize.init] buffer = void;
> }
>
> What's the capacity of buffer upon object construction?
>
> I expect that buffer.length == 4096, since bufferSize == 4096, but get
> buffer.length == 0, since bufferSize.init == int.init.
> It works not as expected and confuses a little. Do I have any chanse this
> will be fixed anytime, or is it intended?
I think this is intended, but you can do this:
class Buffer {
typedef int BufSize_t = 4096;
BufSize_t bufsize;
char[BufSize_t.init] buffer;
};
The code in both cases seems confusing, since you could just use:
class Buffer {
char[4096] buffer;
}
It's a static buffer so the size can't change in any case.
Kevin
| |||
March 29, 2008 Re: Small feature request | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kevin Bealer | On Sat, 29 Mar 2008 21:17:40 +0300, Kevin Bealer <kevinbealer@gmail.com> wrote:
> Koroskin Denis Wrote:
>
> The code in both cases seems confusing, since you could just use:
>
> class Buffer {
> char[4096] buffer;
> }
>
> It's a static buffer so the size can't change in any case.
>
> Kevin
>
Yes, but now it looks like a magic number to me. Moreover, I prefer using constant variables instead of buffer.length every time I need its capacity.
Anyway, my point is that if I initialize my variable like this:
T someVariable = someConstantExpression;
then someVariable.init should evaluate to someConstantExpression hereafter.
It does't break current rules, since
T someOtherVariable;
is identical to:
T someOtherVariable = T.init;
and therefore someOtherVariable.init == T.init.
| |||
March 29, 2008 Re: Small feature request | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Koroskin Denis wrote:
> Look at this class.
>
> class Buffer
> {
> private int bufferSize = 4096;
> private void[bufferSize.init] buffer = void;
> }
>
> What's the capacity of buffer upon object construction?
>
> I expect that buffer.length == 4096, since bufferSize == 4096, but get
> buffer.length == 0, since bufferSize.init == int.init.
> It works not as expected and confuses a little. Do I have any chanse this
> will be fixed anytime, or is it intended?
.init used to do what you want but was changed along the way...
| |||
March 29, 2008 Re: Small feature request | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | On 29/03/2008, Koroskin Denis <2korden@gmail.com> wrote:
> Look at this class.
>
> class Buffer
> {
> private int bufferSize = 4096;
> private void[bufferSize.init] buffer = void;
> }
In the current (D2.012) regime, that should be
class Buffer
{
private enum bufferSize = 4096;
private void[bufferSize] buffer = void;
}
| |||
March 29, 2008 Re: Small feature request | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Koroskin Denis wrote: > Look at this class. > > class Buffer > { > private int bufferSize = 4096; > private void[bufferSize.init] buffer = void; > } > > What's the capacity of buffer upon object construction? > > I expect that buffer.length == 4096, since bufferSize == 4096, but get buffer.length == 0, since bufferSize.init == int.init. > It works not as expected and confuses a little. Do I have any chanse this will be fixed anytime, or is it intended? This is one of the few post-1.0 changes that broke existing code. I really wish it were changed back, but I doubt it will be. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi | |||
March 29, 2008 Re: Small feature request | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | On Sat, 29 Mar 2008 23:01:26 +0300, Janice Caron <caron800@googlemail.com> wrote:
> On 29/03/2008, Koroskin Denis <2korden@gmail.com> wrote:
>> Look at this class.
>>
>> class Buffer
>> {
>> private int bufferSize = 4096;
>> private void[bufferSize.init] buffer = void;
>> }
>
> In the current (D2.012) regime, that should be
>
> class Buffer
> {
> private enum bufferSize = 4096;
> private void[bufferSize] buffer = void;
> }
I would declare it as "static const int" if I needed a constant value.
An example is probably bad but I needed a /mutable/ variable by design.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply