Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EEP 70: Implement non-skipping generators (as an experimental feature) #8625

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Commits on Oct 12, 2024

  1. Configuration menu
    Copy the full SHA
    0f0e375 View commit details
    Browse the repository at this point in the history

Commits on Oct 21, 2024

  1. Implement strict generators

    Currently existing generators are "relaxed": they ignore terms in the
    right-hand side expression that do not match the left-hand side pattern.
    Strict generators on the other hand fail with exception badmatch.
    
    The motivation for strict generators is that relaxed generators can hide
    the presence of unexpected elements in the input data of a
    comprehension. For example consider the below snippet:
    
    [{User, Email} || #{user := User, email := Email} <- all_users()]
    
    This list comprehension would filter out users that don't have an email
    address. This may be an issue if we suspect potentially incorrect input
    data, like in case all_users/0 would read the users from a JSON file.
    Therefore cautious code that would prefer crashing instead of silently
    filtering out incorrect input would have to use a more verbose map
    function:
    
    lists:map(fun(#{user := User, email := Email}) -> {User, Email} end,
              all_users())
    
    Unlike the generator, the anonymous function would crash on a user
    without an email address. Strict generators would allow similar
    semantics in comprehensions too:
    
    [{User, Email} || #{user := User, email := Email} <:- all_users()]
    
    This generator would crash (with a badmatch error) if the pattern
    wouldn't match an element of the list.
    
    Syntactically strict generators use <:- (for lists and maps) and <:=
    (for binaries) instead of <- and <=. This syntax was chosen because
    <:- and <:= somewhat resemble the =:= operator that tests whether two
    terms match, and at the same time keep the operators short and easy to
    type. Having the two types of operators differ by a single character,
    `:`, also makes the operators easy to remember as "`:` means strict."
    dszoboszlay committed Oct 21, 2024
    Configuration menu
    Copy the full SHA
    46f1495 View commit details
    Browse the repository at this point in the history