HigherOrderCO / Kind

A modern proof language
https://higherorderco.com
MIT License
3.55k stars 141 forks source link

Add mutter, setter and getter syntax #483

Closed algebraic-dev closed 1 year ago

algebraic-dev commented 1 year ago
let thing = Thing (Object (V3 0.0 0.0 0.0) (V3 0.0 0.0 0.0))
let thing = !Thing thing .object .pos .x += 10.0         // set
let thing = !Thing thing .object .pos .x @= λx (+ x 1.0) // mut
let thing = !Thing thing .object .pos .x                 // get
VictorTaelin commented 1 year ago

@Derenash what do you think?

Derenash commented 1 year ago

Can I use the get without assigning the value to a variable? Can I do this?

let a = Tup5.new 1 2 3 4 5
let b = Tup5.new 6 5 4 3 (!Tup5 a .snd)

or this?

let a = Tup5.new 1 2 3 4 5
let b = Tup5.new 5 6 7 8 9
let a = !Tup5 a .snd @= λx (+ (!Tup5 b .snd) x) 
VictorTaelin commented 1 year ago

Yep. Both of these should work.

kungfooman commented 1 year ago

Can someone explain mutter in more detail? This is the first time I hear about this concept next to setter/getter.

Is there anything like that in e.g. C++ or JavaScript?

let thing = !Thing thing .object .pos .x @= λx (+ x 1.0) // mut

This seems to be the same as += 1, but also able to create more complex calculations without the tedious a.b.c.d.e.x = fn(a.b.c.d.e.x), simply a.b.c.d.e.x @= fn?

algebraic-dev commented 1 year ago

Yep, the example I gave in the first comment is similar to thing.object.pos.x @= (x => x + 1) or simply thing.object.pos.x += 1. The difference is that we are in a functional language and therefore the data is immutable, so something like !Thing thing .a .b @= f should be translated into a sequence of mutters like Thing.a.mut thing (value_ => OtherThing.b.mut value_ f) instead of a local mutation that would happen in imperative languages.

algebraic-dev commented 1 year ago

Changed the setter syntax to:

 !Thing thing .object .pos .x = 10
VictorTaelin commented 1 year ago

That was fast...