alpaca-lang / alpaca

Functional programming inspired by ML for the Erlang VM
Other
1.44k stars 46 forks source link

Individual record field access #46

Open j14159 opened 7 years ago

j14159 commented 7 years ago

At the moment we can only access members of records via a pattern match but we need to be able to do

let r = {x=1, y=2} in
  r.x + 2

I think this will require rewriting the AST in or before the code generation stage to put a pattern match up front, e.g. in Erlang:

case r of
    #{'__struct__' := record, 'x' := R_x} ->  R_x + 2
yurrriq commented 7 years ago

https://github.com/rvirding/lfe/blob/develop/src/lfe_macro_record.erl might be of interest here. Basically it takes record definitions and generates make- and match- macros (which we don't need in Alpaca) as well macros for getting all fields, and getting and setting individual fields. The compiler could desugar r.x to get_rec_x r.