digitalmars.D.learn - Associative arrays give compile error
- Bob Cowdery (7/7) Oct 05 2010 I can't seem to get any sense out of associative arrays. Even the
- Denis Koroskin (4/11) Oct 05 2010 What exactly compiler version are you using (run dmd with no args)?
- Bob Cowdery (3/18) Oct 05 2010 It says 2.049. How odd. I've got a fair amount of code and everything
- Denis Koroskin (12/33) Oct 05 2010 Can you please post complete code snippet that fails to compile?
- Bob Cowdery (10/44) Oct 05 2010 Ah! It's some other code below it that is not giving an error but
- Denis Koroskin (11/62) Oct 05 2010 import std.stdio;
- Bob Cowdery (47/113) Oct 05 2010 Thanks. I've established that works for me and also that the actual
- Denis Koroskin (27/114) Oct 05 2010 You are trying to declare global variable and initialize at in compile
- Denis Koroskin (3/122) Oct 05 2010 See my other reply for a better solution.
- Bob Cowdery (5/137) Oct 05 2010 Thanks very much. It compiles now. The reason I thought it was an issue
- Denis Koroskin (16/156) Oct 05 2010 "static this" is called a static constructor and can be used for classes...
- Bob Cowdery (4/164) Oct 05 2010 I'm not totally understanding that. Why can enum compute that at compile
- Denis Koroskin (3/173) Oct 05 2010 Let's say it's a limitation of dmd compiler. I'll submit a bug report.
- Steven Schveighoffer (13/33) Oct 05 2010 I'd be very wary of this solution. Recently, enum has been shown to
- Bob Cowdery (6/40) Oct 05 2010 I'm happy with the static this solution so will leave well alone. I
- Bob Cowdery (39/150) Oct 05 2010 Oh dear, this is getting worse and worse. I've still got problems with a
- Lars T. Kyllingstad (44/205) Oct 05 2010 (E_MODE)3,"CWU":cast(E_MODE)4,"FMN":cast(E_MODE)5,"AM":cast(E_MODE)
- Denis Koroskin (4/164) Oct 05 2010 Try using enum A_MODE instead of auto A_MODE, it should help. Sorry for ...
- bearophile (39/79) Oct 05 2010 Now Tuples accept the natural syntax too:
- bearophile (26/58) Oct 05 2010 That code of mine is not good. The following version is more DRY, and in...
- bearophile (3/9) Oct 05 2010 How do you build an immutable AA that is global or local to a function?
- Denis Koroskin (20/29) Oct 05 2010 I found the following to work fine:
- bearophile (4/18) Oct 05 2010 Thank you for your answer. But I need to compute it, so I don't have jus...
- Denis Koroskin (3/22) Oct 05 2010 Compute mutable copy and then cast to immutable. Am I missing something?
- bearophile (4/5) Oct 05 2010 That's possible. But it's an exceptionally dirty thing, I am not sure it...
- Denis Koroskin (3/10) Oct 05 2010 Yes, that's why I'm interested in exploring deep object cloning solution...
- Steven Schveighoffer (10/15) Oct 06 2010 Casting to immutable is the only way to create such a beast. The best w...
- bearophile (5/6) Oct 06 2010 Then maybe we have to improve the language semantics to allow a better s...
- Steven Schveighoffer (8/14) Oct 06 2010 Or, we can make array literals (including AA's) immutable, and call it a...
I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bob
Oct 05 2010
On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On 05/10/2010 11:45, Denis Koroskin wrote:On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On 05/10/2010 12:04, Denis Koroskin wrote:On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On 05/10/2010 12:13, Denis Koroskin wrote:On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; I get an error on every line: Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| ||=== Build finished: 14 errors, 0 warnings ===| This is a bit worrying now. I moved the array into the file that uses it but I still get the same errors. Any ideas?On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 12:13, Denis Koroskin wrote:You are trying to declare global variable and initialize at in compile time. As far as I know, you can't initialize AA at compile time atm (this might be implemented in future though). As such, I'd recommend against using global variables (try moving it to some class or something). Anyway, you need to initialize it at some point, either manually: Tuple!(double,double)[string] A_RX_FILT; void init() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } or automatically at thread startup: static this() { init(); } Hope that helps.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 15:53:55 +0400, Denis Koroskin <2korden gmail.com> wrote:On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:See my other reply for a better solution.On 05/10/2010 12:13, Denis Koroskin wrote:You are trying to declare global variable and initialize at in compile time. As far as I know, you can't initialize AA at compile time atm (this might be implemented in future though). As such, I'd recommend against using global variables (try moving it to some class or something). Anyway, you need to initialize it at some point, either manually: Tuple!(double,double)[string] A_RX_FILT; void init() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } or automatically at thread startup: static this() { init(); } Hope that helps.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On 05/10/2010 13:05, Denis Koroskin wrote:On Tue, 05 Oct 2010 15:53:55 +0400, Denis Koroskin <2korden gmail.com> wrote:Thanks very much. It compiles now. The reason I thought it was an issue was because sometime it did compile a global associative array. I need to do some homework on what 'this' does. It's clearly a powerful concept and has wider application than class constructors.On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:See my other reply for a better solution.On 05/10/2010 12:13, Denis Koroskin wrote:You are trying to declare global variable and initialize at in compile time. As far as I know, you can't initialize AA at compile time atm (this might be implemented in future though). As such, I'd recommend against using global variables (try moving it to some class or something). Anyway, you need to initialize it at some point, either manually: Tuple!(double,double)[string] A_RX_FILT; void init() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } or automatically at thread startup: static this() { init(); } Hope that helps.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 16:32:14 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 13:05, Denis Koroskin wrote:"static this" is called a static constructor and can be used for classes and modules. The code in static constructor is guarantied to be called before you use that class/module, it usually happens upon thread initialization. The other solution is better though: enum A_RX_FILT = [ // just works "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On Tue, 05 Oct 2010 15:53:55 +0400, Denis Koroskin <2korden gmail.com> wrote:Thanks very much. It compiles now. The reason I thought it was an issue was because sometime it did compile a global associative array. I need to do some homework on what 'this' does. It's clearly a powerful concept and has wider application than class constructors.On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:See my other reply for a better solution.On 05/10/2010 12:13, Denis Koroskin wrote:You are trying to declare global variable and initialize at in compile time. As far as I know, you can't initialize AA at compile time atm (this might be implemented in future though). As such, I'd recommend against using global variables (try moving it to some class or something). Anyway, you need to initialize it at some point, either manually: Tuple!(double,double)[string] A_RX_FILT; void init() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } or automatically at thread startup: static this() { init(); } Hope that helps.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On 05/10/2010 13:45, Denis Koroskin wrote:On Tue, 05 Oct 2010 16:32:14 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:I'm not totally understanding that. Why can enum compute that at compile time and the thing which it is, an associative array cannot. Is it to do with where these things live.On 05/10/2010 13:05, Denis Koroskin wrote:"static this" is called a static constructor and can be used for classes and modules. The code in static constructor is guarantied to be called before you use that class/module, it usually happens upon thread initialization. The other solution is better though: enum A_RX_FILT = [ // just works "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On Tue, 05 Oct 2010 15:53:55 +0400, Denis Koroskin <2korden gmail.com> wrote:Thanks very much. It compiles now. The reason I thought it was an issue was because sometime it did compile a global associative array. I need to do some homework on what 'this' does. It's clearly a powerful concept and has wider application than class constructors.On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:See my other reply for a better solution.On 05/10/2010 12:13, Denis Koroskin wrote:You are trying to declare global variable and initialize at in compile time. As far as I know, you can't initialize AA at compile time atm (this might be implemented in future though). As such, I'd recommend against using global variables (try moving it to some class or something). Anyway, you need to initialize it at some point, either manually: Tuple!(double,double)[string] A_RX_FILT; void init() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } or automatically at thread startup: static this() { init(); } Hope that helps.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 17:00:13 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 13:45, Denis Koroskin wrote:Let's say it's a limitation of dmd compiler. I'll submit a bug report.On Tue, 05 Oct 2010 16:32:14 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:I'm not totally understanding that. Why can enum compute that at compile time and the thing which it is, an associative array cannot. Is it to do with where these things live.On 05/10/2010 13:05, Denis Koroskin wrote:"static this" is called a static constructor and can be used for classes and modules. The code in static constructor is guarantied to be called before you use that class/module, it usually happens upon thread initialization. The other solution is better though: enum A_RX_FILT = [ // just works "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On Tue, 05 Oct 2010 15:53:55 +0400, Denis Koroskin <2korden gmail.com> wrote:Thanks very much. It compiles now. The reason I thought it was an issue was because sometime it did compile a global associative array. I need to do some homework on what 'this' does. It's clearly a powerful concept and has wider application than class constructors.On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:See my other reply for a better solution.On 05/10/2010 12:13, Denis Koroskin wrote:You are trying to declare global variable and initialize at in compile time. As far as I know, you can't initialize AA at compile time atm (this might be implemented in future though). As such, I'd recommend against using global variables (try moving it to some class or something). Anyway, you need to initialize it at some point, either manually: Tuple!(double,double)[string] A_RX_FILT; void init() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } or automatically at thread startup: static this() { init(); } Hope that helps.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 09:00:13 -0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 13:45, Denis Koroskin wrote:I'd be very wary of this solution. Recently, enum has been shown to construct itself on every use. So what I think is happening is every time you use A_RX_FILT, it's building a brand new AA (you can verify this by looking at the disassembly). I'd recommend the static this solution in order to ensure you are not accidentally killing performance by just using that AA. A while back, Don suggested that all literals should be considered immutable. I agree with him, but Walter still doesn't. If all literals are immutable, then they could be truly constructed at compile-time. -Steve"static this" is called a static constructor and can be used for classes and modules. The code in static constructor is guarantied to be called before you use that class/module, it usually happens upon thread initialization. The other solution is better though: enum A_RX_FILT = [ // just works "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];I'm not totally understanding that. Why can enum compute that at compile time and the thing which it is, an associative array cannot. Is it to do with where these things live.
Oct 05 2010
On 05/10/2010 15:14, Steven Schveighoffer wrote:On Tue, 05 Oct 2010 09:00:13 -0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:I'm happy with the static this solution so will leave well alone. I generally keep mutable and immutable data structures apart so I tend to agree that literals should be immutable although of course I'm not aware of the pros and cons. bobOn 05/10/2010 13:45, Denis Koroskin wrote:I'd be very wary of this solution. Recently, enum has been shown to construct itself on every use. So what I think is happening is every time you use A_RX_FILT, it's building a brand new AA (you can verify this by looking at the disassembly). I'd recommend the static this solution in order to ensure you are not accidentally killing performance by just using that AA. A while back, Don suggested that all literals should be considered immutable. I agree with him, but Walter still doesn't. If all literals are immutable, then they could be truly constructed at compile-time. -Steve"static this" is called a static constructor and can be used for classes and modules. The code in static constructor is guarantied to be called before you use that class/module, it usually happens upon thread initialization. The other solution is better though: enum A_RX_FILT = [ // just works "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ];I'm not totally understanding that. Why can enum compute that at compile time and the thing which it is, an associative array cannot. Is it to do with where these things live.
Oct 05 2010
On 05/10/2010 12:40, Bob Cowdery wrote:On 05/10/2010 12:13, Denis Koroskin wrote:Oh dear, this is getting worse and worse. I've still got problems with a simple definition. If I take out the one with the tuple and leave in this one: enum E_MODE { LSB, // 0 USB, // 1 DSB, // 2 CWL, // 3 CWU, // 4 FMN, // 5 AM, // 6 DIGU, // 7 SPEC, // 8 DIGL, // 9 SAM, // 10 DRM // 11 } // Associative array for translation auto A_MODE = [ "LSB": E_MODE.LSB, "USB": E_MODE.USB, "DSB": E_MODE.DSB, "CWL": E_MODE.CWL, "CWU": E_MODE.CWU, "FMN": E_MODE.FMN, "AM": E_MODE.AM, "DIGU": E_MODE.DIGU, "SPEC": E_MODE.SPEC, "DIGL": E_MODE.DIGL, "SAM": E_MODE.SAM, "DRM": E_MODE.DRM ]; I get: Definitions\dspDefs.d|25|Error: non-constant expression ["LSB":cast(E_MODE)0,"USB":cast(E_MODE)1,"DSB":cast(E_MODE)2,"CWL":cast(E_MODE)3,"CWU":cast(E_MODE)4,"FMN":cast(E_MODE)5,"AM":cast(E_MODE)6,"DIGU":cast(E_MODE)7,"SPEC":cast(E_MODE)8,"DIGL":cast(E_MODE)9,"SAM":cast(E_MODE)10,"DRM":cast(E_MODE)11]| ||=== Build finished: 1 errors, 0 warnings ===| Something is seriously broken here.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; I get an error on every line: Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| ||=== Build finished: 14 errors, 0 warnings ===| This is a bit worrying now. I moved the array into the file that uses it but I still get the same errors. Any ideas?On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
On Tue, 05 Oct 2010 12:50:44 +0100, Bob Cowdery wrote:On 05/10/2010 12:40, Bob Cowdery wrote:(E_MODE)3,"CWU":cast(E_MODE)4,"FMN":cast(E_MODE)5,"AM":cast(E_MODE) 6,"DIGU":cast(E_MODE)7,"SPEC":cast(E_MODE)8,"DIGL":cast(E_MODE) 9,"SAM":cast(E_MODE)10,"DRM":cast(E_MODE)11]|On 05/10/2010 12:13, Denis Koroskin wrote:Oh dear, this is getting worse and worse. I've still got problems with a simple definition. If I take out the one with the tuple and leave in this one: enum E_MODE { LSB, // 0 USB, // 1 DSB, // 2 CWL, // 3 CWU, // 4 FMN, // 5 AM, // 6 DIGU, // 7 SPEC, // 8 DIGL, // 9 SAM, // 10 DRM // 11 } // Associative array for translation auto A_MODE = [ "LSB": E_MODE.LSB, "USB": E_MODE.USB, "DSB": E_MODE.DSB, "CWL": E_MODE.CWL, "CWU": E_MODE.CWU, "FMN": E_MODE.FMN, "AM": E_MODE.AM, "DIGU": E_MODE.DIGU, "SPEC": E_MODE.SPEC, "DIGL": E_MODE.DIGL, "SAM": E_MODE.SAM, "DRM": E_MODE.DRM ]; I get: Definitions\dspDefs.d|25|Error: non-constant expression ["LSB":cast(E_MODE)0,"USB":cast(E_MODE)1,"DSB":cast(E_MODE)2,"CWL":castOn Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; I get an error on every line: Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| ||=== Build finished: 14 errors, 0 warnings ===| This is a bit worrying now. I moved the array into the file that uses it but I still get the same errors. Any ideas?On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).||=== Build finished: 1 errors, 0 warnings ===| Something is seriously broken here.No, nothing is broken. :) The problem is that you are trying to create an associative array at compile time, which isn't possible. This works: void main() { auto foo = [ "Hello" : "World" ]; // Array created at run time writeln(foo); } This doesn't work: auto foo = [ "Hello" : "World" ]; // Cannot create AA at compile time void main() { writeln(foo); } To create a module-level AA which is initialised on program start, use a module constructor: string[string] foo; static this() { foo = [ "Hello" : "World" ]; } void main() { writeln(foo); } In your case, this would mean: Tuple!(double, double)[string] A_RX_FILT; static this() { A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; } -Lars
Oct 05 2010
On Tue, 05 Oct 2010 15:50:44 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:On 05/10/2010 12:40, Bob Cowdery wrote:Try using enum A_MODE instead of auto A_MODE, it should help. Sorry for confusion.On 05/10/2010 12:13, Denis Koroskin wrote:Oh dear, this is getting worse and worse. I've still got problems with a simple definition. If I take out the one with the tuple and leave in this one: enum E_MODE { LSB, // 0 USB, // 1 DSB, // 2 CWL, // 3 CWU, // 4 FMN, // 5 AM, // 6 DIGU, // 7 SPEC, // 8 DIGL, // 9 SAM, // 10 DRM // 11 } // Associative array for translation auto A_MODE = [ "LSB": E_MODE.LSB, "USB": E_MODE.USB, "DSB": E_MODE.DSB, "CWL": E_MODE.CWL, "CWU": E_MODE.CWU, "FMN": E_MODE.FMN, "AM": E_MODE.AM, "DIGU": E_MODE.DIGU, "SPEC": E_MODE.SPEC, "DIGL": E_MODE.DIGL, "SAM": E_MODE.SAM, "DRM": E_MODE.DRM ]; I get: Definitions\dspDefs.d|25|Error: non-constant expression ["LSB":cast(E_MODE)0,"USB":cast(E_MODE)1,"DSB":cast(E_MODE)2,"CWL":cast(E_MODE)3,"CWU":cast(E_MODE)4,"FMN":cast(E_MODE)5,"AM":cast(E_MODE)6,"DIGU":cast(E_MODE)7,"SPEC":cast(E_MODE)8,"DIGL":cast(E_MODE)9,"SAM":cast(E_MODE)10,"DRM":cast(E_MODE)11]| ||=== Build finished: 1 errors, 0 warnings ===| Something is seriously broken here.On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Thanks. I've established that works for me and also that the actual array I'm using also works in the test program but it won't compile in the real program. I've commented everything else out of the file and just left... import std.typecons; auto A_RX_FILT = [ "6K0": tuple(100.0, 6100.0), "2K4": tuple(300.0, 2700.0), "2K1": tuple(300.0, 2400.0), "1K0": tuple(300.0, 1300.0), "500": tuple(500.0, 1000.0), "250": tuple(600.0, 850.0), "100": tuple(700.0, 800.0) ]; I get an error on every line: Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(100,6100) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2700) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,2400) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(300,1300) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(500,1000) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(600,850) at compile time| Definitions\dspDefs.d|51|Error: cannot evaluate tuple(700,800) at compile time| ||=== Build finished: 14 errors, 0 warnings ===| This is a bit worrying now. I moved the array into the file that uses it but I still get the same errors. Any ideas?On 05/10/2010 12:04, Denis Koroskin wrote:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:Ah! It's some other code below it that is not giving an error but causing the error above. So the compiler is getting confused. What I was actually trying to do was create an associative array with a string as a key and a Tuple as the value. Now auto aa = [ "some string": (100.0, 6100.0) ] compiles but is clearly wrong and gives rise to other errors. Does anyone know the correct way to define this and then access the tuple.On 05/10/2010 11:45, Denis Koroskin wrote:Can you please post complete code snippet that fails to compile? Here is the code I used to test: module aa; import std.stdio; void main() { int[string] aa = ["hello":42]; writeln(aa["hello"]); }On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery <bob bobcowdery.plus.com> wrote:It says 2.049. How odd. I've got a fair amount of code and everything else compiles fine.I can't seem to get any sense out of associative arrays. Even the simplest definition won't compile so I must be doing something daft. int[string] aa = ["hello":42]; Error: non-constant expression ["hello":42] What exactly is not constant about this. The example is straight out the book. Using D 2.0. bobWhat exactly compiler version are you using (run dmd with no args)? Works perfectly fine here (dmd2.049).
Oct 05 2010
Denis Koroskin:import std.stdio; import std.typecons; void main() { auto aa = ["hello": tuple(100.0, 6100.0)]; auto result = aa["hello"]; writeln(result.field[0], " ", result._1); // primary and alternative way }Now Tuples accept the natural syntax too: writeln(result[0], " ", result[1]); ---------------------------------------- Bob Cowdery:enum E_MODE { LSB, // 0 USB, // 1 DSB, // 2 CWL, // 3 CWU, // 4 FMN, // 5 AM, // 6 DIGU, // 7 SPEC, // 8 DIGL, // 9 SAM, // 10 DRM // 11 } // Associative array for translation auto A_MODE = [ "LSB": E_MODE.LSB, "USB": E_MODE.USB, "DSB": E_MODE.DSB, "CWL": E_MODE.CWL, "CWU": E_MODE.CWU, "FMN": E_MODE.FMN, "AM": E_MODE.AM, "DIGU": E_MODE.DIGU, "SPEC": E_MODE.SPEC, "DIGL": E_MODE.DIGL, "SAM": E_MODE.SAM, "DRM": E_MODE.DRM ];I suggest code similar to: enum E_MODE { LSB, // 0 USB, // 1 DSB, // 2 CWL, // 3 CWU, // 4 FMN, // 5 AM, // 6 DIGU, // 7 SPEC, // 8 DIGL, // 9 SAM, // 10 DRM // 11 } void main() { // associative array for translation with (E_MODE) immutable auto a_mode = [ "LSB": LSB, "USB": USB, "DSB": DSB, "CWL": CWL, "CWU": CWU, "FMN": FMN, "AM": AM, "DIGU": DIGU, "SPEC": SPEC, "DIGL": DIGL, "SAM": SAM, "DRM": DRM ]; } Bye, bearophile
Oct 05 2010
enum E_MODE { LSB, // 0 USB, // 1 DSB, // 2 CWL, // 3 CWU, // 4 FMN, // 5 AM, // 6 DIGU, // 7 SPEC, // 8 DIGL, // 9 SAM, // 10 DRM // 11 } void main() { // associative array for translation with (E_MODE) immutable auto a_mode = [ "LSB": LSB, "USB": USB, "DSB": DSB, "CWL": CWL, "CWU": CWU, "FMN": FMN, "AM": AM, "DIGU": DIGU, "SPEC": SPEC, "DIGL": DIGL, "SAM": SAM, "DRM": DRM ]; }That code of mine is not good. The following version is more DRY, and in theory it's a better, but in practice it doesn't work: import std.stdio: writeln; enum E_MODE { LSB, USB, DSB, CWL, CWU, FMN, AM, DIGU, SPEC, DIGL, SAM, DRM } /*immutable*/ E_MODE[string] a_mode; static this () { foreach (m; __traits(allMembers, E_MODE)) mixin(`a_mode["` ~ m ~ `"] = E_MODE.` ~ m ~ `;`); } void main() { writeln(a_mode); } Bye, bearophile
Oct 05 2010
/*immutable*/ E_MODE[string] a_mode; static this () { foreach (m; __traits(allMembers, E_MODE)) mixin(`a_mode["` ~ m ~ `"] = E_MODE.` ~ m ~ `;`); }How do you build an immutable AA that is global or local to a function? Bye, bearophile
Oct 05 2010
On Wed, 06 Oct 2010 03:45:11 +0400, bearophile <bearophileHUGS lycos.com> wrote:I found the following to work fine: K[V] assocArray = createAssocArray(); K[V] createAssocArray() { K[V] assocArray = [ k1: v1, k2: v2, ... ]; return assocArray; } No idea why the following: K[V] assocArray = [ k1: v1, k2: v2, ... ]; doesn't work directly for global scope./*immutable*/ E_MODE[string] a_mode; static this () { foreach (m; __traits(allMembers, E_MODE)) mixin(`a_mode["` ~ m ~ `"] = E_MODE.` ~ m ~ `;`); }How do you build an immutable AA that is global or local to a function? Bye, bearophile
Oct 05 2010
Denis Koroskin:I found the following to work fine: K[V] assocArray = createAssocArray(); K[V] createAssocArray() { K[V] assocArray = [ k1: v1, k2: v2, ... ]; return assocArray; }Thank you for your answer. But I need to compute it, so I don't have just an AA literal. And I'd like it to be immutable :-) Bye, bearophile
Oct 05 2010
On Wed, 06 Oct 2010 04:14:37 +0400, bearophile <bearophileHUGS lycos.com> wrote:Denis Koroskin:Compute mutable copy and then cast to immutable. Am I missing something?I found the following to work fine: K[V] assocArray = createAssocArray(); K[V] createAssocArray() { K[V] assocArray = [ k1: v1, k2: v2, ... ]; return assocArray; }Thank you for your answer. But I need to compute it, so I don't have just an AA literal. And I'd like it to be immutable :-) Bye, bearophile
Oct 05 2010
Denis Koroskin:Compute mutable copy and then cast to immutable. Am I missing something?That's possible. But it's an exceptionally dirty thing, I am not sure it works in SafeD. A well designed const system has to offer a more clean solution :-) Do you agree? Bye, bearophile
Oct 05 2010
On Wed, 06 Oct 2010 06:02:30 +0400, bearophile <bearophileHUGS lycos.com> wrote:Denis Koroskin:Yes, that's why I'm interested in exploring deep object cloning solution.Compute mutable copy and then cast to immutable. Am I missing something?That's possible. But it's an exceptionally dirty thing, I am not sure it works in SafeD. A well designed const system has to offer a more clean solution :-) Do you agree? Bye, bearophile
Oct 05 2010
On Tue, 05 Oct 2010 22:02:30 -0400, bearophile <bearophileHUGS lycos.com> wrote:Denis Koroskin:Casting to immutable is the only way to create such a beast. The best way to ensure as few bugs as possible is to create the object you wish to be immutable in a function, and cast at the end before returning. This at least encapsulates the dirtiness in one function (which would probably be marked trusted). You can also use assumeUnique (which I'm guessing is there so it can be called in safeD?). -SteveCompute mutable copy and then cast to immutable. Am I missing something?That's possible. But it's an exceptionally dirty thing, I am not sure it works in SafeD. A well designed const system has to offer a more clean solution :-) Do you agree?
Oct 06 2010
Steven Schveighoffer:Casting to immutable is the only way to create such a beast.Then maybe we have to improve the language semantics to allow a better solution. See the Transients of Clojure or the larval objects of Java :-) The compiler may need to test (at compile time) that there's only one reference to the AA and its contents, and turn it into immutable. Bye, bearophile
Oct 06 2010
On Wed, 06 Oct 2010 08:12:28 -0400, bearophile <bearophileHUGS lycos.com> wrote:Steven Schveighoffer:Or, we can make array literals (including AA's) immutable, and call it a day. The only thing is that AA literals provide a nice syntax to make non-immutable AAs also, and I can't think of a good way to make a library function that creates an AA. Anyone have any ideas? -SteveCasting to immutable is the only way to create such a beast.Then maybe we have to improve the language semantics to allow a better solution. See the Transients of Clojure or the larval objects of Java :-) The compiler may need to test (at compile time) that there's only one reference to the AA and its contents, and turn it into immutable.
Oct 06 2010