digitalmars.D.learn - Tango BitArray Initialization
- Colin Huang (11/11) Feb 10 2007 When reading the Tango documentation, I came across this piece of code:
- Sean Kelly (16/35) Feb 11 2007 Nope. The documentation for tango.core was the first to be written an
- John Reimer (15/31) Feb 11 2007 The Doc is innacurrate in this respect on both
- Colin Huang (4/5) Feb 11 2007 Even if an opCall overload were in place, I doubt that the last line wou...
- Sean Kelly (12/23) Feb 11 2007 I don't think this is legal in D. In fact, the syntax is a huge problem...
- Colin Huang (3/17) Feb 11 2007 Why is this useful? Isn't "int x = 1" enough?
- Sean Kelly (14/35) Feb 11 2007 Templates. It still isn't perfect because classes can't be initialized
- Colin Huang (3/43) Feb 12 2007 Ah, that makes sense. Should've thought of it :)
- John Reimer (13/44) Feb 11 2007 Apparently I missed the issue completely, Sorry. I'm not sure how or wh...
- Bill Baxter (7/20) Feb 11 2007 Does opAssign work that way? I think it has to be done in two lines:
- Sean Kelly (3/26) Feb 12 2007 Oddly, if you make the opCall static, it works.
- Bill Baxter (17/46) Feb 12 2007 Hmm. Show me how. This and several variations of this that I tried do
- Bill Baxter (22/70) Feb 12 2007 Oh, wait. "if you make the *opCall* static". Weird. This does work:
- kris (2/86) Feb 12 2007 No opCall needed. Just return the struct from the static opAssign() inst...
- Sean Kelly (8/96) Feb 13 2007 Oddly, I wasn't able to get static opAssign to work with this syntax.
- Colin Huang (3/14) Feb 13 2007 Good work :)
- Sean Kelly (8/23) Feb 13 2007 By the way, I think the above will break when implicit type conversions
- Thomas Brix Larsen (8/34) Feb 13 2007 Can't you just make it a ubyte array, and throw an error if it isn't fil...
- Sean Kelly (3/36) Feb 13 2007 Yup, and this is what will probably happen.
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).
Feb 10 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
Feb 11 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
Feb 11 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
Feb 11 2007
Colin Huang wrote:John Reimer Wrote:Windows).(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 onI 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. thxI 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
Feb 11 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?
Feb 11 2007
Colin Huang wrote:Sean Kelly Wrote: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. SeanI 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?
Feb 11 2007
Sean Kelly Wrote:Colin Huang wrote:Ah, that makes sense. Should've thought of it :) ColinSean Kelly Wrote: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.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?
Feb 12 2007
On Sun, 11 Feb 2007 19:41:48 -0800, Sean Kelly wrote:Colin Huang wrote: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. -JJRJohn Reimer Wrote:> compile. It certainly didn't last time I tried (I'm using gdc 0.22 on Windows).(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 wouldI 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, atleast). If it actually works, pls give some examples. thxI 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
Feb 11 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
Feb 11 2007
Bill Baxter wrote:John Reimer wrote:Oddly, if you make the opCall static, it works. SeanOn 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'".
Feb 12 2007
Sean Kelly wrote:Bill Baxter wrote: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); } --bbJohn Reimer wrote:Oddly, if you make the opCall static, it works. SeanOn 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'".
Feb 12 2007
Bill Baxter wrote:Sean Kelly wrote: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! --bbBill Baxter wrote: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); }John Reimer wrote:Oddly, if you make the opCall static, it works. SeanOn 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'".
Feb 12 2007
Bill Baxter wrote:Bill Baxter wrote:No opCall needed. Just return the struct from the static opAssign() insteadSean Kelly wrote: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! --bbBill Baxter wrote: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); }John Reimer wrote:Oddly, if you make the opCall static, it works. SeanOn 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'".
Feb 12 2007
kris wrote:Bill Baxter 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. SeanBill Baxter wrote:No opCall needed. Just return the struct from the static opAssign() insteadSean Kelly wrote: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! --bbBill Baxter wrote: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); }John Reimer wrote:Oddly, if you make the opCall static, it works. SeanOn 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'".
Feb 13 2007
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. SeanGood work :) Colin
Feb 13 2007
Colin Huang wrote:Sean Kelly Wrote: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. SeanOddly, 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. SeanGood work :)
Feb 13 2007
Sean Kelly wrote:Colin Huang wrote: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. ;)Sean Kelly Wrote: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. SeanOddly, 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. SeanGood work :)
Feb 13 2007
Thomas Brix Larsen wrote:Sean Kelly wrote:Yup, and this is what will probably happen. SeanColin Huang wrote: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!!");Sean Kelly Wrote: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. SeanOddly, 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. SeanGood work :)
Feb 13 2007