gavr123456789 / Niva

Smalltalk like programming language
https://gavr123456789.github.io/niva-site
41 stars 2 forks source link

Make mutation only possible from methods declared for mut types #303

Open gavr123456789 opened 3 weeks ago

gavr123456789 commented 3 weeks ago
Person name: String
p = Person name: "Alice" 

Person sas = name <- "Kate" // ERROR, Person is immutable
mut Person sas = name <- "Kate" // all good

It would be cool to have a better way to mark reciever as mutable.
But it looks good with extend syntax:

extend mut Person [
  on sas = name <- "Kate"
]

Also, maybe the fact that it looks bad is actually a good thing?, since hallo, mutations are bad.
This change will make all the mutations local to the very root of evaluation.
Wanna mutate something somewhere? You shoud have a mutable reference there.

gavr123456789 commented 3 weeks ago
mut person = Person age: 42
person msgThatMutatesAge // all good
person = Person age: 42
person birthday // ERROR, person is immutable, declare mut

mut Person birthday = [
  age <- age inc
]
gavr123456789 commented 2 weeks ago

All methods that mutate must end with !

This can be an alternative to methods for mut types, or addition.

Person birthday = [
  age <- age + 1 // ERROR, rename method to birthday!
]

person birthday! // now u see its mutates

Person somethind = [
  this birthday! // ERROR, birthday mutates so u need to rename to somethind!
]

Pros: