Tango BitArray Initialization

Bill Baxter dnewsgroup at billbaxter.com
Mon Feb 12 00:41:08 PST 2007


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


More information about the Digitalmars-d-learn mailing list