Thread overview | |||||
---|---|---|---|---|---|
|
January 21, 2007 Initializing Arrays | ||||
---|---|---|---|---|
| ||||
Hi. I'm new to D but not new to programming. I had a few questions regarding arrays and their initialization: // celsius.init will be 100, the boiling point of water typedef float celsius = 100.00; If I declare an array of type celsius to group the low, average, and high temperature: celsius[3] temps; I'd like to be able to default the elements to something other than the value of celsius.init: a syntax like celsius[3] temps = [0, 20, 18]; In the global namespace (outside of main) the gdc compiler doesn't have any issues with this and gives me what I expect, temps[0] = 0.0, temps[1] = 20.0, and temps[2] = 18.0. Within main however I get errors such as: arrayex.d:22: Error: cannot implicitly convert expression ([0,20,18]) of type int[3] to celsius. Is there a way to uniquely specify the default value of each element of an array in this manner in a non-global namespace? Many thanks for any insight. Joe |
January 21, 2007 Re: Initializing Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Bell | Not sure if it'll work, but try using [cast(celcius)0,20,18]. Casting the first element will change the type of the array. L. "Joseph Bell" <josephabell@tx.rr.com> wrote in message news:ep0h1h$18ph$1@digitaldaemon.com... > Hi. > > I'm new to D but not new to programming. I had a few questions regarding arrays and their initialization: > > // celsius.init will be 100, the boiling point of water typedef float celsius = 100.00; > > If I declare an array of type celsius to group the low, average, and high temperature: > > celsius[3] temps; > > I'd like to be able to default the elements to something other than the value of celsius.init: a syntax like > > celsius[3] temps = [0, 20, 18]; > > In the global namespace (outside of main) the gdc compiler doesn't have any issues with this and gives me what I expect, temps[0] = 0.0, temps[1] = 20.0, and temps[2] = 18.0. Within main however I get errors such as: > > arrayex.d:22: Error: cannot implicitly convert expression ([0,20,18]) of type int[3] to celsius. > > Is there a way to uniquely specify the default value of each element of an array in this manner in a non-global namespace? > > Many thanks for any insight. > > Joe |
January 21, 2007 Re: Initializing Arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | That does indeed work as expected. The following also works: celsius[3] temps = cast(celsius[3])[0, 20, 18]; and as I expect the following works okay as well: int[3] ints = [1, 2, 3]; I find it curious though that celsius[3] temps = [70,80,90] compiles cleanly in the global namespace and not in the main function. Any idea why? Lionello Lunesu wrote: > Not sure if it'll work, but try using [cast(celcius)0,20,18]. > Casting the first element will change the type of the array. > > L. > > "Joseph Bell" <josephabell@tx.rr.com> wrote in message news:ep0h1h$18ph$1@digitaldaemon.com... >> Hi. >> >> I'm new to D but not new to programming. I had a few questions regarding arrays and their initialization: >> >> // celsius.init will be 100, the boiling point of water >> typedef float celsius = 100.00; >> >> If I declare an array of type celsius to group the low, average, and high temperature: >> >> celsius[3] temps; >> >> I'd like to be able to default the elements to something other than the value of celsius.init: a syntax like >> >> celsius[3] temps = [0, 20, 18]; >> >> In the global namespace (outside of main) the gdc compiler doesn't have any issues with this and gives me what I expect, temps[0] = 0.0, temps[1] = 20.0, and temps[2] = 18.0. Within main however I get errors such as: >> >> arrayex.d:22: Error: cannot implicitly convert expression ([0,20,18]) of type int[3] to celsius. >> >> Is there a way to uniquely specify the default value of each element of an array in this manner in a non-global namespace? >> >> Many thanks for any insight. >> >> Joe > > |
Copyright © 1999-2021 by the D Language Foundation