[Naming convention to mutation methods] whether allow `mut` on immutable methods
mut can already be used to indicate that it will modify the object, just like in function arguments, with the difference that it is not required. Example:
mut a := [1 2 4]
(mut a).insert(2, 3)
// or
a.insert(2, 3)
the question now becomes whether mut can be used too to immutable methods like:
a := [1 2 4]
f := a.first()
// or
f := (mut a).first() // too works
Mutation methods
Ruby and Julia use ! in end of mutation methods, in V could be adopted the pattern of use either:
__mutation_mutable_mut_mt
in end of a mutation method
Example, either:
sort_()
sort_mutation()
sort_mutable()
sort_mut()
sort_mt()
Non-Mutation methods
or as opposite, to use a pattern to naming non-mutation methods using either:
__clone_cln
Example, either:
sort_()
sort_clone()
sort_cln()
related: https://github.com/vlang/v/discussions/13631
mut can already be used to indicate that it will modify the object, just like in function arguments, with the difference that it is not required. Example:
mut a := [1 2 4]
(mut a).insert(2, 3)
// or
a.insert(2, 3)
the question now becomes whether mut can be used too to immutable methods like:
a := [1 2 4]
f := a.first()
// or
f := (mut a).first() // too works
Another way could be add a a.mut().first()?
No. Immutable objects should stay immutable for the entire run of the program. If you want something to be mutable, declare it mutable in the first place.
If you really want to play games with an immutable object, you can do so in an unsafe block... but on your head be it - don't complain that V is doing something wrong.
a := [1 2 4]
f := a.first()
// or
f := (mut a).first() // too works
How about
f := mut a.first()
// or
f := a!.first()
I like the idea of the bang operator, also because it is not new stuff in the programming syntax, like the null safety in the dart, kotlin, swift, and the unwrap in rust.
Just worried about that this can cause small confusion between people that use different languages
@elimisteve @vincenzopalazzo the bang operator ! would interfere with e.g. maps (e.g. mymap['abc']!) and potentially also with sum types (see also https://github.com/vlang/v/pull/12912 ).
So whatever will be decided here, it must not be ! :wink:.
@vincenzopalazzo @elimisteve I'm ok with (mut a).insert(2, 3) and I didn't like a.mut().insert(2, 3) or a!.insert(2, 3). The discussion is: how .first() don't change the array, what's the meaning and use for (mut a).first() and whether should cause a warning or error
how .first() don't change the array, what's the meaning and use for (mut a).first() and whether should cause a warning or error
I think we need some smart mechanism to change the array, and I think rust has a good solution with a mutable iterator, but we need to reason on it about how to integrate it on V if we want to take into consideration this opportunity.