[erlang-questions] Nested Record Pattern Matching in R14A

Tuncer Ayaz tuncer.ayaz@REDACTED
Sat Jul 17 23:29:09 CEST 2010


On Thu, Jul 8, 2010 at 3:34 PM, Brian Williams wrote:
> I converted a small search algorithm project to use the new nested
> record syntax in R14A.
> My records are:
> -record(state, {yard_state}).
> -record(solution_state, {state = #state{}, moves, depth}).
> -record(astar_solution_state, {solution_state = #solution_state{}, fvalue}).
>
> I was able to use a line like this to access a field in the nested record:
> Astar_Solution_State#astar_solution_state.solution_state#solution_state.state
>
> Based on the example in the reference manual, I thought the pattern
> for matching the state field would be:
>
> function_call(#astar_solution_state.solution_state#solution_state{state
> = State})

Hi Brian,

this is invalid and it's not how records are matched.

It almost looks like an expression to update a record field:
foo(Astar) when is_record(Astar, astar_solution_state) ->
  State = #state{yard_state=foo_yard},
  Astar#astar_solution_state.solution_state#solution_state{state=State}.

> However, that gave me "illegal pattern" errors, and after a bit of
> fiddling, I got this to work:
> #astar_solution_state{solution_state = #solution_state{state = State}}
>
> Is this intended? Or a bug?

It is intended.


More information about the erlang-questions mailing list