constants_and_enumerations.rst 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. .. _constants_and_enumerations:
  2. ========================
  3. Constants & Enumerations
  4. ========================
  5. .. index::
  6. single: Constants & Enumerations
  7. Squirrel allows to bind constant values to an identifier that will be evaluated compile-time.
  8. This is achieved though constants and Enumerations.
  9. ---------------
  10. Constants
  11. ---------------
  12. .. index::
  13. single: Constants
  14. Constants bind a specific value to an identifier. Constants are similar to
  15. global values, except that they are evaluated compile time and their value cannot be changed.
  16. constants values can only be integers, floats or string literals. No expression are allowed.
  17. are declared with the following syntax.::
  18. const foobar = 100;
  19. const floatbar = 1.2;
  20. const stringbar = "I'm a constant string";
  21. constants are always globally scoped, from the moment they are declared, any following code
  22. can reference them.
  23. Constants will shadow any global slot with the same name( the global slot will remain visible by using the ``::`` syntax).::
  24. local x = foobar * 2;
  25. ---------------
  26. Enumerations
  27. ---------------
  28. .. index::
  29. single: Enumerations
  30. As Constants, Enumerations bind a specific value to a name. Enumerations are also evaluated at compile time
  31. and their value cannot be changed.
  32. An enum declaration introduces a new enumeration into the program.
  33. Enumeration values can only be integers, floats or string literals. No expression are allowed.::
  34. enum Stuff {
  35. first, //this will be 0
  36. second, //this will be 1
  37. third //this will be 2
  38. }
  39. or::
  40. enum Stuff {
  41. first = 10
  42. second = "string"
  43. third = 1.2
  44. }
  45. An enum value is accessed in a manner that's similar to accessing a static class member.
  46. The name of the member must be qualified with the name of the enumeration, for example ``Stuff.second``
  47. Enumerations will shadow any global slot with the same name( the global slot will remain visible by using the ``::`` syntax).::
  48. local x = Stuff.first * 2;
  49. --------------------
  50. Implementation notes
  51. --------------------
  52. Enumerations and Constants are a compile-time feature. Only integers, string and floats can be declared as const/enum;
  53. No expressions are allowed(because they would have to be evaluated compile time).
  54. When a const or an enum is declared, it is added compile time to the ``consttable``. This table is stored in the VM shared state
  55. and is shared by the VM and all its threads.
  56. The ``consttable`` is a regular squirrel table; In the same way as the ``roottable``
  57. it can be modified runtime.
  58. You can access the ``consttable`` through the built-in function ``getconsttable()``
  59. and also change it through the built-in function ``setconsttable()``
  60. here some example: ::
  61. //creates a constant
  62. getconsttable()["something"] <- 10"
  63. //creates an enumeration
  64. getconsttable()["somethingelse"] <- { a = "10", c = "20", d = "200"};
  65. //deletes the constant
  66. delete getconsttable()["something"]
  67. //deletes the enumeration
  68. delete getconsttable()["somethingelse"]
  69. This system allows to procedurally declare constants and enumerations, it is also possible to assign any squirrel type
  70. to a constant/enumeration(function,classes etc...). However this will make serialization of a code chunk impossible.