Variable Bindings proposal discussion

I like this proposal, some minor points:

  1. I think using the same default arg convention of functions can be used for the “for” bindings which is:
  • read in fn
  • read in def unless written to then var.

I think it is better to optimize for the common case when you only want to read and don’t want to copy, this will be more performant by default and shorter.
This doesn’t necessarily mean we need to allow explicit read/mut as all of the variations can be achieved:

  • default is read
  • use ref when you need to mutate instead of mut
  • use var when you need a copy.
  1. About allowing explicit mut/read in fn var declarations, the only use case I see is to prevent accidental mutation, for example when passing to another function, but the same case could be made for var vs let declaration which mojo has already decided is not necessary, so for consistency and simplicity I think it is better to only allow ref and potentially use some casting to prevent accidental mutation if really needed similar to var.
var x = my_list[0]
external_function(x) # could mutate
external_function(read x) # immutable cast - not sure about syntax or if needed

ref y = my_list[0]
external_function(y) # could mutate
external_function(read y) # immutable cast - not sure about syntax or if needed

read z = my_list[0] 
external_function(z) # prevent accidental mutation
  1. I’m not convinced about this syntax:
# Declare two uninitialized variables
var a2, b2 : Int, String

The other alternative is to put the annotation next to the variable:

# Declare two uninitialized variables
var a2: Int, b2: String

The advantages are:

  • it is easier to see which type belong to which variable when there are many of them and potentially unpacking in the future.
  • it can allow for partial type definitions:
var x: String, y, z = ["a", "b", "c"] # y and z are auto inferred
var x, y, z : String, StaticString, StaticString =  ["a", "b", "c"] # forced to also annotate y and z 

As a minor note python disallow this:

x, y: int, int
    ^
#SyntaxError: only single target (not tuple) can be annotated