5 ms·
> While in an FP language you are passing values By passing values do you mean 'moving'? Like not passing reference?
by timeon 1y ago
> While in an FP language you are passing values
By passing values do you mean 'moving'? Like not passing reference?
- zelphirkalt 1y agoYes, I guess in Rust terms, that is called moving. However, when I have some code that "moves" the value into another procedure, then the code after that call, can no longer use the moved value. So I want to move a value, but also be able to use it after moving it, because I don't mutate it in that other function, where it got moved to. So it is actually more like copying, but without making a copy in memory. It would be good, if Rust realized, that I don't have mutating calls anywhere and just lets me use the value. When I have a mutation going on, then of course the compiler should throw error, because that would be unsafe business.
- asa400 1y agoI'm not sure how what you're describing is different from passing an immutable/shared reference. If you call `foo(&value)` then `value` remains available in your calling scope after `foo` returns. If you don't mutate `value` in foo, and foo doesn't do anything other than derive a new value from `value`, then it sounds like a shared reference works for what you're describing? Rust makes you be explicit as to whether you want to lend out the value or give the value away, which is a design decision, and Rust chooses that the bare syntax `value` is for moving and the `&value` syntax is for borrowing. Perhaps you're arguing that a shared immutable borrow should be the default syntax. Apologies if I'm misunderstanding!
- NIckGeek 1y agoCouldn't you just pass a reference to your value (i.e. `&T`)? If you absolutely _need_ ownership the function you call could return back the owned value or you could use one of the mechanisms for shared ownership like `Rc<T>`. In a GC'd functional language, you're effectively getting the latter (although usually a different form of GC instead of reference counting)
- zelphirkalt 1y agoI think I could. But then I would need to put & in front of every argument in every procedure definition and also deal with working with references inside the procedure, with the syntax that brings along.
- Mond_ 1y agoFair to be annoyed by this, but not very interesting: This is just a minor syntactical pattern that exists for a very good reason. Syntax is generally the least interesting/important part of languages.
- pepa65 1y agoWhen you pass &variable, I don't think it affects the syntax inside the called function, does it?
- asa400 1y agoCorrect. If you then want to subsequently re-reference or dereference that reference (this happens sometimes), you'll need to accordingly `&` or `*` it, but if you're just using it as is, the bare syntactical `name` (whatever it happens to be) already refers to a reference.
- gf000 1y agoAlso, Rust does implicit dereferencing, so it's not that much of an issue in practice.