Skip to content

Commit

Permalink
Error index entry for L1318
Browse files Browse the repository at this point in the history
Summary:
The "update entry" error is un-intuitive, especially in presence of macros. Circumventing the warning via a `begin/end` block is also not intuitive, so let's make the information discoverable by developers.

If we find a better way to circumvent the warning, we can update this page.

Reviewed By: alanz

Differential Revision: D58005331

fbshipit-source-id: 838cbb2160cf04b338684c6ceceb6eb8cd3d63b4
  • Loading branch information
robertoaloi authored and facebook-github-bot committed Jun 26, 2024
1 parent 8402121 commit d82f86f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions website/docs/erlang-error-index/l/L1318.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
sidebar_position: 318
---

# L1318 - Expression Updates a Literal

## Error

```erlang
-define(DEFAULT, #{a => 1}).

updated(Value) ->
?DEFAULT#{a => Value}.
%% ^^^^^^^^ warning: expression updates a literal
```

## Explanation

The warning occurs when a map or a record is updated using the following syntaxes:

```erlang
> #{a => b}#{c => d}
#{c => d,a => b}
```

```erlang
> rd(my_record, {a, b}). %% rd/2 allows you to define an Erlang record from a shell
> #my_record{a = 1}#my_record{a = 2}.
#my_record{a = 2,b = undefined}
```

While this is valid Erlang syntax, this behaviour is usually not intentional and the result of a missing comma in a list of elements. Consider, for example:

```erlang
my_list() ->
[ #{a => 1} %% Missing comma here!
#{a => 2}
].
```

Which results in `[#{a => 2}]`.

To fix the issue, just add the missing comma. If the update is intentional, a common (but ugly) workaround to silent the linter is to wrap the first map/record in a `begin/end` block,
which will avoid any additional runtime cost. As an example, you could rewrite the following:

```erlang
-define(DEFAULT, #{a => 1}).

updated(Value) ->
?DEFAULT#{a => Value}.
```

Into:

```erlang
-define(DEFAULT, #{a => 1}).

updated(Value) ->
begin ?DEFAULT end#{a => Value}.
```

0 comments on commit d82f86f

Please sign in to comment.