Skip to content

Allow pattern matching (or guards) on native records module/name #10861

@josevalim

Description

@josevalim

Is your feature request related to a problem? Please describe.

With native records, it is currently not possible to pattern match on the record module/name. One suggestion is to introduce the following syntax:

#RecordModule:RecordName{...}

In case you are interested only on the module or the name, you can use _. Note that writing #_:_{...} ends up being the same as #_{...}.

Describe alternatives you've considered

A potential alternative is to introduce new guards that allows getting the record module and the record name. The guard approach may actually be preferred if there are plans to introduce guards that other guards, such as record_get/2 to get the value of a record field. This way we could have record_get/2, record_name/1, and record_module/1 as guards.

If guards are desired, perhaps some of the functions in the records module should be moved to guards before Erlang/OTP 29 to avoid duplication. On the other hand, duplication is not necessarily a problem, as that already happens with the maps module.

Use cases

While adding any of the above will bring additional features for matching on records, they would be essential for potentially migrating Elixir structs to native records, as it is possible today to write %struct_name{...} in Elixir, which is not yet possible with native records.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    OTBTo be discussed in OTP Technical Boardenhancementteam:LGAssigned to OTP language group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions