odin-lang / odin-lang.org

http://odin-lang.org
26 stars 80 forks source link

Clarified meaning of "constant" for `$` parameters #103

Closed WraithGlade closed 1 year ago

WraithGlade commented 1 year ago

I'm still new to Odin, so I'm not 100% sure, but I've tested this behavior and the edited description I've given here seems more accurate and clarifying.

The previous vague mention of "constant" was highly confusing and didn't paint the pull picture clearly enough.

WraithGlade commented 1 year ago

You guys would know better than me of course. 🙂

Feel free to change my commit and adjust it however you want. I won't take it personally.

I do think that the section I changed needs clarification though, in at least some form.

Also, I corrected a normal typo in there too by the way.

Anyway though, I need to work on something else for a few hours now and so won't be available for a couple hours at least.

Have a good night!

On Thu, Mar 16, 2023 at 6:35 PM Jeroen van Rijn @.***> wrote:

@.**** commented on this pull request.

In content/docs/overview.md https://github.com/odin-lang/odin-lang.org/pull/103#discussion_r1139423576 :

-To specify that a parameter is "constant", the parameters name must be prefixed with a dollar sign $. The following example takes two constant parameters to initialize an array of known length: +As a reminder, all parameters passed into a function are run-time "constants" by default in the sense that they can't have their value changed using = directly. Yet, if instead one wishes to use parameter names mutably, then one must indicate so using := like so: + +```odin +sin_tau :: proc(angle_in_cycles: f64) -> f64 {

  • angle_in_cycles := angle_in_cycles // Enables treating angle_in_cycles mutably!
  • TAU :: 2 * math.PI
  • angle_in_cycles *= TAU
  • return math.sin(angle_in_cycles) +} +assert(math.abs(sin_tau(0.25) - 1) <= 0.001) // sin_tau(0.25) is approximately 1 +assert(math.abs(sin_tau(0.75) - -1) <= 0.001) // sin_tau(0.75) is approximately -1 +```
  • +However, to specify that a parameter must be a compile-time constant, which is not the same thing as a run-time constant and may sometimes be necessary or desirable, the parameter's name must be prefixed with a dollar sign $. The following example takes two compile-time constant parameters and then uses them to initialize an array of known length:

There's no such thing as a run-time constant as all constants are resolved at compile time. There are read-only variables, like e.g.:

FILE_DATA :: #load("filename") file_data := FILE_DATA // read-only global containing file data. It is a variable, but trying to write to it will segfault (if you work around the compiler errors that tell you you can't mutate it.)

— Reply to this email directly, view it on GitHub https://github.com/odin-lang/odin-lang.org/pull/103#pullrequestreview-1344853950, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB7ZBFIYHRPFCEKC64SUMBLW4OISHANCNFSM6AAAAAAV5ZURXQ . You are receiving this because you authored the thread.Message ID: @.***>

WraithGlade commented 1 year ago

Cool, thanks for improving the text I added! It's even clearer to me now. 🙂

On Fri, Mar 17, 2023 at 2:28 AM Jeroen van Rijn @.***> wrote:

Merged #103 https://github.com/odin-lang/odin-lang.org/pull/103 into master.

— Reply to this email directly, view it on GitHub https://github.com/odin-lang/odin-lang.org/pull/103#event-8774834636, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB7ZBFICEKWE2ROTNWVLIXDW4P775ANCNFSM6AAAAAAV5ZURXQ . You are receiving this because you authored the thread.Message ID: @.***>

Kelimion commented 1 year ago

I'm glad to hear it.