Understanding the Notation Variance in Elixir Type Specifications

57
clicks
Understanding the Notation Variance in Elixir Type Specifications

Source: reddit.com

Type: Discussion

In Elixir, when defining types within a module, developers can use two notations, `t` and `t()`. It's common to encounter both forms in various examples, including the Elixir standard library, which might use `String.t` or `String.t()`. This inconsistency has left some developers pondering about the preferred convention or if there's a difference between the two. According to a community member's comment by Sentreen, both notations are effectively the same, as they refer to invoking the `t/0` type function of the module. The parentheses in Elixir function calls are optional; however, trends within the Elixir community have shifted towards using explicit parentheses for clarity. This rise in explicit syntax is supported by the `mix format` command, which adjusts the code to use parentheses by default. Therefore, `t()` is now the more accepted form, suggesting that a shift towards explicitness has occurred as the language evolved.

© HashMerge 2024