Jump to page: 1 2
Thread overview
Tango BitArray Initialization
Feb 10, 2007
Colin Huang
Feb 11, 2007
Sean Kelly
Feb 12, 2007
John Reimer
Feb 12, 2007
Colin Huang
Feb 12, 2007
Sean Kelly
Feb 12, 2007
Colin Huang
Feb 12, 2007
Sean Kelly
Feb 12, 2007
Colin Huang
Feb 12, 2007
John Reimer
Feb 12, 2007
Bill Baxter
Feb 12, 2007
Sean Kelly
Feb 12, 2007
Bill Baxter
Feb 12, 2007
Bill Baxter
Feb 12, 2007
kris
Feb 13, 2007
Sean Kelly
Feb 13, 2007
Colin Huang
Feb 13, 2007
Sean Kelly
Feb 13, 2007
Thomas Brix Larsen
Feb 13, 2007
Sean Kelly
February 10, 2007
When reading the Tango documentation, I came across this piece of code:

import tango.core.BitArray;

void main()
{
    BitArray bitbag1 = bits( 1,0,1,1,1,0 );
    // or
    BitArray bitbag2( 1,0,1,1,1,0 );
}

The documentation says that "bitbag2 is initialized such that it uses a special opCall overload for the BitArray".

Now the problem is that when I look at the source code, I don't see any overload of opCall for BitArray, or any definition for the bits function. Am I missing anything here?

BTW, is the last line legal D code at all? It doesn't look like it's gonna compile (to me, at least).
February 11, 2007
Colin Huang wrote:
> When reading the Tango documentation, I came across this piece of code:
> 
> import tango.core.BitArray;
> 
> void main()
> {
>     BitArray bitbag1 = bits( 1,0,1,1,1,0 );
>     // or
>     BitArray bitbag2( 1,0,1,1,1,0 );
> }
> 
> The documentation says that "bitbag2 is initialized such that it uses a special
> opCall overload for the BitArray".
> 
> Now the problem is that when I look at the source code, I don't see any overload
> of opCall for BitArray, or any definition for the bits function. Am I
> missing anything here?

Nope.  The documentation for tango.core was the first to be written an as a result it may be a bit inaccurate in places.  It would be easy to provide such a function however... something like this:

    BitArray bits( bool[] mask ... )
    {
        BitArray temp;

        temp.length = mask.length;
        foreach( pos, val; mask )
            temp[pos] = val;
        return temp;
    }

It may be even better to simply make this a ctor for BitArray, so:

    auto b = BitArray( 1, 0, 1, 0 );

> BTW, is the last line legal D code at all? It doesn't look like it's gonna
> compile (to me, at least).

The last line doesn't seem correct.  I'll give the docs a look tomorrow.


Sean
February 12, 2007
On Sat, 10 Feb 2007 09:10:07 -0500, Colin Huang wrote:

> When reading the Tango documentation, I came across this piece of code:
> 
> import tango.core.BitArray;
> 
> void main()
> {
>     BitArray bitbag1 = bits( 1,0,1,1,1,0 );
>     // or
>     BitArray bitbag2( 1,0,1,1,1,0 );
> }
> 
> The documentation says that "bitbag2 is initialized such that it uses a special opCall overload for the BitArray".
> 
> Now the problem is that when I look at the source code, I don't see any overload of opCall for BitArray, or any definition for the bits function. Am I missing anything here?
> 
> BTW, is the last line legal D code at all? It doesn't look like it's gonna compile (to me, at least).


The Doc is innacurrate in this respect on both
counts.  The "bits" function and "opCall" do not exist.

When I wrote it, I was hoping that Tango would implement a "simple"
way to initialize the BitArray.  There were a number of options at the
time, but nothing happened in this case because there were greater
priorities. The examples above were intended to be suggestions for easier
initialization syntax. What bothered me at the time was that there was no
simple single line initialization of the BitArray... it involved two or
more steps and more lines of code: creation of the bitarray and then
initialization. What I was looking for was a simple one line
declaration/initialization solution.

(The last line could be legal if an opCall were implemented.)

We'll get the doc fixed for now and explore the possibilities a little more.

-JJR
February 12, 2007
John Reimer Wrote:

> (The last line could be legal if an opCall were implemented.)

Even if an opCall overload were in place, I doubt that the last line would compile. It certainly didn't last time I tried (I'm using gdc 0.22 on Windows). I may be wrong on this, of course.

In fact, I've been wanting to be able to do that for quite some time now, so I can use structs like stack-allocated objects in C++ (syntax-wise, at least). If it actually works, pls give some examples. thx

Colin

February 12, 2007
Colin Huang wrote:
> John Reimer Wrote:
> 
>> (The last line could be legal if an opCall were implemented.)
> 
> Even if an opCall overload were in place, I doubt that the last line would
> compile. It certainly didn't last time I tried (I'm using gdc 0.22 on Windows).
> I may be wrong on this, of course.
> 
> In fact, I've been wanting to be able to do that for quite some time now, so I
> can use structs like stack-allocated objects in C++ (syntax-wise, at
> least).   If it actually works, pls give some examples. thx

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.  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 );


Sean
February 12, 2007
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?

February 12, 2007
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.


Sean
February 12, 2007
On Sun, 11 Feb 2007 19:41:48 -0800, Sean Kelly wrote:

> Colin Huang wrote:
>> John Reimer Wrote:
>> 
>>> (The last line could be legal if an opCall were implemented.)
>> 
>> Even if an opCall overload were in place, I doubt that the last line would
>  > compile. It certainly didn't last time I tried (I'm using gdc 0.22 on
> Windows).
>> I may be wrong on this, of course.
>> 
>> In fact, I've been wanting to be able to do that for quite some time now, so I
>  > can use structs like stack-allocated objects in C++ (syntax-wise, at
>> least).   If it actually works, pls give some examples. thx
> 
> 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.
>   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 );
> 
> 
> Sean


Apparently I missed the issue completely, Sorry.  I'm not sure how or why I came up with the notation in the first place.

What I'm actually interested in seeing is a simple solution here for assignment of a binary type.

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.

-JJR
February 12, 2007
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'".

--bb
February 12, 2007
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
« First   ‹ Prev
1 2