Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear section in Overview docs #213

Open
PabloIsCoding opened this issue Jun 1, 2024 · 2 comments
Open

Unclear section in Overview docs #213

PabloIsCoding opened this issue Jun 1, 2024 · 2 comments

Comments

@PabloIsCoding
Copy link

https://github.com/odin-lang/odin-lang.org/blob/56ba2d01a1ea53a02b934b70733e9fb04ce8a864/content/docs/overview.md?plain=1#L670C1-L680C4

Hi, I was reading the Odin language overview and I found the above a bit confusing. A few reasons:

  1. The way I understand this, if you pass arguments by value to a function, you are making a copy. It's when you use a pointer like in C that you do not copy. So why do the docs say that you need to generate a copy to have the "pointer behavior" (so to speak)?
  2. If what it's meant is that Odin first makes a copy of the argument, and then you have to copy back the original thing that was passed, how is that done by a line like x := x?
  3. Does this imply some computational penalty because we are making copies?

Since I don't really understand how this works I can't say how this should be rephrased (if at all) but I thought it might be helpful to give some feedback on this.

@Skytrias
Copy link
Collaborator

Here's how i understand it:

  1. It's not really "pointer behavior" it's just that x := x wouldn't bee necessary in C. It's more meant to show that you can't mutate the passed parameter, unless it contains pointers like the slice, etc mentioned
  2. You already get copies, it's just that you can't mutate them
  3. As stated in the docs above the highlighted code, it should be good for optimizations

@gingerBill can explain this better

@gingerBill
Copy link
Member

If you come from C, all parameters do this copy implicitly.

void foo(int x) { 
    x = 123; // I can change it
}

x in this case is a variable copy, and you can mutate it.

In Odin, you have explicitly copy the parameter to state you want to mutate it in some way.

foo :: proc(x: i32) {
    x = 123 // INVALID
}
foo :: proc(x: i32) {
    x := x
    x = 123 // allowed since `x` has been explicitly copied
}

As for (3), yes and Odin makes this clear. It wasn't clear in C before, even if in many cases the optimizer can remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants