View mode: basic / threaded / horizontal-split · Log in · Help
February 12, 2007
Re: Tango BitArray Initialization
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);
}



--bb
February 12, 2007
Re: Tango BitArray Initialization
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
February 12, 2007
Re: Tango BitArray Initialization
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
February 12, 2007
Re: Tango BitArray Initialization
Sean Kelly Wrote:

> Colin Huang wrote:
> > Sean Kelly Wrote:
> > 
> >> I don't think this is legal in D.  In fact, the syntax is a huge problem 
> >> in C++ because the parser often can't distinguish between a variable 
> >> decl and a function prototype, and function prototypes take precedence. 
> > 
> > Yeah, that got me really confused when I first started learning C++
> > 
> >>   It may be uglier, but:
> >>
> >>      BitArray b = BitArray( 1, 0, 1 );
> >>
> >> is better than:
> >>
> >>      BitArray b( 1, 0, 1 );
> >>
> >> I only wish that the syntax worked for all stack variables.  ie.
> >>
> >>      int x = int( 1 );
> > 
> > Why is this useful? Isn't "int x = 1" enough?
> 
> Templates.  It still isn't perfect because classes can't be initialized 
> this way, but:
> 
>      void buildAndCall(T, Call, Params...)( Call fn, Params p )
>      {
>          T val = T( p );
>          fn( val );
>      }
> 
> Probably a bad example, but you get the idea.  It's nice to be able to 
> initialize everything using the same syntax.  I'd almost suggest:
> 
>      scope T val = new T( p );
> 
> as the "universal" scoped declaration syntax, but the presence of the 
> 'new' is not terribly ideal, even though it should work for classes as well.
> 

Ah, that makes sense. Should've thought of it :)

Colin
February 13, 2007
Re: Tango BitArray Initialization
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
February 13, 2007
Re: Tango BitArray Initialization
Sean Kelly Wrote:

> 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

Good work :)

Colin
February 13, 2007
Re: Tango BitArray Initialization
Colin Huang wrote:
> Sean Kelly Wrote:
> 
>> 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
> 
> Good work :)

By the way, I think the above will break when implicit type conversions 
are added, because [0,1,0,1] is currently passed as a bool[].  I'll need 
to think about this a bit more, since [true,false,true,false] kind of 
stinks, as does accepting an array of ubyte or some such.  This is an 
issue Walter will have to face as well since Phobos BitArray will have 
the same problem.


Sean
February 13, 2007
Re: Tango BitArray Initialization
Sean Kelly wrote:

> Colin Huang wrote:
>> Sean Kelly Wrote:
>> 
>>> 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
>> 
>> Good work :)
> 
> By the way, I think the above will break when implicit type conversions
> are added, because [0,1,0,1] is currently passed as a bool[].  I'll need
> to think about this a bit more, since [true,false,true,false] kind of
> stinks, as does accepting an array of ubyte or some such.  This is an
> issue Walter will have to face as well since Phobos BitArray will have
> the same problem.
> 
> 
> Sean

Can't you just make it a ubyte array, and throw an error if it isn't filled
with ones and zeros?

Something like:
if(...
  throw new Error("Stupid human! Bits are made up of ones and zeros!!");

- Brix

ps. ;)
February 13, 2007
Re: Tango BitArray Initialization
Thomas Brix Larsen wrote:
> Sean Kelly wrote:
> 
>> Colin Huang wrote:
>>> Sean Kelly Wrote:
>>>
>>>> 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
>>> Good work :)
>> By the way, I think the above will break when implicit type conversions
>> are added, because [0,1,0,1] is currently passed as a bool[].  I'll need
>> to think about this a bit more, since [true,false,true,false] kind of
>> stinks, as does accepting an array of ubyte or some such.  This is an
>> issue Walter will have to face as well since Phobos BitArray will have
>> the same problem.
>>
>>
>> Sean
> 
> Can't you just make it a ubyte array, and throw an error if it isn't filled
> with ones and zeros?
> 
> Something like:
> if(...
>    throw new Error("Stupid human! Bits are made up of ones and zeros!!");

Yup, and this is what will probably happen.


Sean
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home