Tango BitArray Initialization

Sean Kelly sean at f4.ca
Tue Feb 13 00:08:41 PST 2007


kris wrote:
> Bill Baxter wrote:
>> Bill Baxter wrote:
>>
>>> Sean Kelly wrote:
>>>
>>>> Bill Baxter wrote:
>>>>
>>>>> John Reimer wrote:
>>>>>
>>>>>> On Sun, 11 Feb 2007 19:41:48 -0800, Sean Kelly wrote:
>>>>>>
>>>>>
>>>>>> Another simple alternative could employ a static opAssign.
>>>>>>
>>>>>> This would make things much simpler:
>>>>>>
>>>>>> BitArray bitbag = 0b11111000000;
>>>>>>
>>>>>> The value is limited to 64-bits, but at least it's clean and 
>>>>>> simple for
>>>>>> those situations where we don't have a long initialization value.
>>>>>> (this would work for hexidecimal value also).  For any larger 
>>>>>> values we
>>>>>> can use an array literal assignment or something similar.
>>>>>>
>>>>>
>>>>> Does opAssign work that way?  I think it has to be done in two lines:
>>>>>
>>>>>  BitArray bitbag;
>>>>>  bitbag = 0b11111000000;
>>>>>
>>>>> Yes that does seem to be the case.  Otherwise you get, bizarrely, 
>>>>> the error "no property 'opCall' for type 'Foo'".
>>>>
>>>>
>>>> Oddly, if you make the opCall static, it works.
>>>>
>>>>
>>>> Sean
>>>
>>>
>>> Hmm.  Show me how.  This and several variations of this that I tried 
>>> do not work:
>>>
>>> import std.stdio;
>>> struct Struct
>>> {
>>>     static void opAssign(int i) {
>>>         val = i;
>>>     }
>>>     int val = 0;
>>> }
>>>
>>> void main()
>>> {
>>>     Struct s = 2;
>>>     writefln("S.val=%s", s.val);
>>> }
>>
>>
>> Oh, wait.  "if you make the *opCall* static".  Weird.  This does work:
>>
>> import std.stdio;
>>
>> struct Struct
>> {
>>     static Struct opCall(int i) {
>>         Struct s;
>>         s.val = i;
>>         return s;
>>     }
>>     void opAssign(int i) {
>>         val = i;
>>     }
>>     int val = 0;
>> }
>>
>> void main()
>> {
>>     Struct s = 2;
>>     writefln("S.val=%s", s.val);
>> }
>>
>> Neat!
>> --bb
> 
> 
> No opCall needed. Just return the struct from the static opAssign() instead

Oddly, I wasn't able to get static opAssign to work with this syntax. 
It seemed to want "BitArray = 0" rather than "BitArray b = 0".  I've 
modified Tango's BitArray to support:

     BitArray b = [0,1,0,1];
     b = [1,0,1,0];

which is consistent with how built-in arrays work.


Sean


More information about the Digitalmars-d-learn mailing list