rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

[Naming convention to mutation methods] whether allow `mut` on immutable methods

Open igotfr opened this issue 4 years ago • 9 comments

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()

igotfr avatar Sep 10 '21 06:09 igotfr

related: https://github.com/vlang/v/discussions/13631

igotfr avatar Mar 02 '22 08:03 igotfr

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

igotfr avatar Mar 11 '22 10:03 igotfr

Another way could be add a a.mut().first()?

vincenzopalazzo avatar Mar 11 '22 13:03 vincenzopalazzo

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.

JalonSolov avatar Mar 11 '22 13:03 JalonSolov

a := [1 2 4]
f := a.first()
// or
f := (mut a).first() // too works

How about

f := mut a.first()

// or

f := a!.first()

elimisteve avatar Mar 11 '22 14:03 elimisteve

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

vincenzopalazzo avatar Mar 11 '22 15:03 vincenzopalazzo

@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:.

dumblob avatar Mar 11 '22 17:03 dumblob

@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

igotfr avatar Mar 14 '22 14:03 igotfr

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.

vincenzopalazzo avatar Mar 23 '22 11:03 vincenzopalazzo