### Start Nix Shell Source: https://hexdocs.pm/typedstruct/contributing Starts a Nix shell manually in the project directory. This is an alternative to using direnv for environment setup. ```bash cd typedstruct nix-shellcopy ``` -------------------------------- ### Install Nix Source: https://hexdocs.pm/typedstruct/contributing Installs Nix, a package manager, by downloading and running an installation script. Nix is used for setting up the development environment. ```bash curl https://nixos.org/nix/install | shcopy ``` -------------------------------- ### Run Git-flow Setup Script Source: https://hexdocs.pm/typedstruct/contributing Executes a setup script for Git-flow, configuring the project for standard Git-flow branching strategies. ```bash ./.gitsetup copy ``` -------------------------------- ### Install direnv Source: https://hexdocs.pm/typedstruct/contributing Installs direnv, a tool that automatically loads and unloads environment variables. It's optional and helps in managing the development environment. ```bash nix-env -i direnvcopy ``` -------------------------------- ### TypedStruct: Struct Definition Example (Elixir) Source: https://hexdocs.pm/typedstruct/changelog A basic example demonstrating how to define a struct using TypedStruct, including fields, types, and default values. ```elixir defmodule UserProfile do use TypedStruct typedstruct do username: [type: String.t(), default: "guest"] email: [type: String.t()] age: [type: integer(), default: 18] is_active: [type: boolean(), default: true] end end # Creating instances: # user1 = %UserProfile{email: "test@example.com"} # user2 = %UserProfile{username: "admin", email: "admin@example.com", age: 42, is_active: false} # Accessing fields: # IO.puts(user1.username) # Output: guest # IO.puts(user2.age) # Output: 42 ``` -------------------------------- ### Allow direnv in Project Source: https://hexdocs.pm/typedstruct/contributing Allows direnv to manage the environment for the current project directory. This is used if direnv is installed and configured. ```bash cd typedstruct direnv allowcopy ``` -------------------------------- ### TypedStruct Field Definition Example Source: https://hexdocs.pm/typedstruct/TypedStruct Provides a basic example of defining a field within a TypedStruct. It shows the syntax for specifying the field name and its type, along with an example comment. ```elixir # A field named :example of type String.t() field :example, String.t() ``` -------------------------------- ### Configure Shell for direnv Source: https://hexdocs.pm/typedstruct/contributing Configures the shell to use direnv. Replace `` with your shell's name (e.g., bash, zsh). This enables automatic environment setup. ```bash eval "$(direnv hook )" ``` -------------------------------- ### Start Feature Branch with Git-flow Source: https://hexdocs.pm/typedstruct/contributing Initiates a new feature branch using the Git-flow branching model. This is suitable for features requiring more extensive work. ```bash git flow feature start copy ``` -------------------------------- ### TypedStruct: Type Definition Example (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Illustrates how to define custom types within TypedStruct using Elixir's type specifications. ```elixir defmodule Product do use TypedStruct # Define custom types using @type @type price :: {integer(), atom()} # Represents {amount, currency_symbol} @type sku :: String.t() typedstruct do name: [type: String.t()] sku: [type: sku(), default: "SKU-000"] price: [type: price()] quantity: [type: integer(), default: 0] end end # Example usage: # product = %Product{name: "Laptop", price: {1200, :USD}, quantity: 5} # IO.inspect(product.price) # Output: {1200, :USD} ``` -------------------------------- ### TypedStruct Mix Dependency Setup Source: https://hexdocs.pm/typedstruct/index Instructions for adding TypedStruct as a dependency in your Elixir project's `mix.exs` file. It shows the basic dependency tuple and mentions the option to disable runtime compilation if not needed. ```elixir {:typedstruct, "~> 0.5"} ``` -------------------------------- ### Format Code with Mix (Elixir) Source: https://hexdocs.pm/typedstruct/contributing Formats Elixir code according to the project's coding style guide using Mix. This ensures consistency across the codebase. ```elixir mix format ``` -------------------------------- ### Define a Typed Record Source: https://hexdocs.pm/typedstruct/TypedStruct Shows the definition of a typed record using the `typedrecord` macro. This example defines a `:person` record with `name` and `age` fields, including a default value for `age` and documentation. ```elixir defmodule Person do use TypedStruct typedrecord :person do @typedoc "A person" field :name, String.t() field :age, non_neg_integer(), default: 0 end end ``` ```elixir defmodule Person do use Record Record.defrecord(:person, name: nil, age: 0) @type person :: {:person, String.t()|nil, non_neg_integer()} end ``` -------------------------------- ### Document Submodule TypedStruct Source: https://hexdocs.pm/typedstruct/readme This example shows how to document a typed struct defined within a submodule. It includes `@moduledoc` for the submodule and `@typedoc` for the struct itself, alongside field definitions. ```Elixir typedstruct module: MyStruct do @moduledoc "A submodule with a typed struct." @typedoc "A typed struct in a submodule" field :a_string, String.t() field :an_int, integer() endcopy ``` -------------------------------- ### TypedStruct: Default Values Example (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Shows how to specify default values for fields in a TypedStruct. Fields without explicit values will use their defined defaults. ```elixir defmodule Task do use TypedStruct typedstruct do title: [type: String.t()] description: [type: String.t(), default: "No description"] priority: [type: integer(), default: 1] completed: [type: boolean(), default: false] end end # Creating a task with only a title: # task1 = %Task{title: "Buy groceries"] # IO.inspect(task1) # %Task{completed: false, description: "No description", priority: 1, title: "Buy groceries"} # Creating a task with all fields specified: # task2 = %Task{title: "Write report", description: "Finalize Q3 report", priority: 5, completed: true} # IO.inspect(task2) # %Task{completed: true, description: "Final Q3 report", priority: 5, title: "Write report"} ``` -------------------------------- ### TypedStruct with Opaque Type Source: https://hexdocs.pm/typedstruct/readme This example illustrates using the `opaque: true` option within a `typedstruct` block. This option replaces the `@type` directive with `@opaque` in the generated type specification for the struct. ```Elixir typedstruct opaque: true do field :name, String.t() endcopy ``` -------------------------------- ### TypedStruct with a Single Field Source: https://hexdocs.pm/typedstruct/readme This example defines a TypedStruct with a single field `:name` of type `String.t()`. It illustrates how a field without options results in a nullable type and a default `nil` value in the struct definition. ```Elixir defmodule Example do use TypedStruct typedstruct do field :name, String.t() end endcopy ``` -------------------------------- ### Define TypedStruct with a Single Field Source: https://hexdocs.pm/typedstruct/TypedStruct Demonstrates defining a TypedStruct with a single field, `:name`, of type `String.t()`. This example shows how the field is added to the `defstruct` with a default `nil` value and its type becomes nullable. ```elixir defmodule Example do use TypedStruct typedstruct do field :name, String.t() end end ``` ```elixir defmodule Example do @enforce_keys [] defstruct name: nil @type t() :: %__MODULE__{ name: String.t() | nil } end ``` -------------------------------- ### Clone TypedStruct Repository Source: https://hexdocs.pm/typedstruct/contributing Clones the TypedStruct repository from GitHub to a local machine. This is the first step in setting up a local development environment. ```bash git clone https://github.com/you/typedstruct.git cd typedstructcopy ``` -------------------------------- ### Checkout Develop Branch Source: https://hexdocs.pm/typedstruct/contributing Switches the local repository to the 'develop' branch, which is used for ongoing development. ```bash git checkout developcopy ``` -------------------------------- ### Fetch Project Dependencies (Elixir) Source: https://hexdocs.pm/typedstruct/contributing Fetches project dependencies using Mix, Elixir's build tool. This command downloads and compiles the necessary libraries for the project. ```elixir cd typedstruct mix deps.getcopy ``` -------------------------------- ### TypedStruct: Plugin API Introduction (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Introduces a plugin API for TypedStruct, enabling extensibility. Previously available reflection functions (`__keys__/0`, `__defaults__/0`, `__types__/0`) have been removed and can be re-enabled via a plugin. ```elixir # Example of how a plugin might be used or defined: # To re-enable reflection functions, you might use a plugin like this: # defmodule TypedStructLegacyReflection do # use TypedStruct.Plugin # # ... implementation for __keys__/0, __defaults__/0, __types__/0 ... # end # In your application or specific module: # use TypedStruct, plugins: [TypedStructLegacyReflection] # Or directly within a typedstruct definition: # defmodule MyStruct do # use TypedStruct, plugins: [TypedStructLegacyReflection], # field1: [type: integer()] # end # Note: The actual implementation details of the plugin API are not shown here, # but this illustrates the concept of using plugins for extensibility. ``` -------------------------------- ### Run Static Analyzers (Elixir) Source: https://hexdocs.pm/typedstruct/contributing Runs static analysis tools on the project using Mix. This helps ensure code quality and identify potential issues before testing. ```elixir mix checkcopy ``` -------------------------------- ### Extend TypedStruct with Plugins Source: https://hexdocs.pm/typedstruct/TypedStruct Shows how to extend TypedStruct functionality using its plugin interface, specifically with `TypedStructLens` to automatically generate lenses. This allows for more dynamic and integrated struct manipulation. ```elixir defmodule MyStruct do use TypedStruct typedstruct do plugin TypedStructLens field :a_field, String.t() field :other_field, atom() end @spec change(t()) :: t() def change(data) do lens = a_field() put_in(data, [lens], "Changed") end end ``` -------------------------------- ### Configure formatter.exs for TypedStruct Source: https://hexdocs.pm/typedstruct/index This snippet shows how to configure your `.formatter.exs` file to prevent `mix format` from adding unnecessary parentheses around TypedStruct field definitions, improving code readability. ```elixir [ ..., import_deps: [:typedstruct] ] ``` -------------------------------- ### Define TypedStruct with Fields and Options Source: https://hexdocs.pm/typedstruct/TypedStruct Defines a typed struct with various fields, specifying their types, enforcement, and default values. Supports options like `enforce`, `visibility`, `module`, and `new`. ```elixir defmodule MyStruct do use TypedStruct typedstruct do field :field_one, String.t() field :field_two, integer(), enforce: true field :field_three, boolean(), enforce: true field :field_four, atom(), default: :hey end end ``` ```elixir defmodule MyStruct do use TypedStruct typedstruct enforce: true do field :field_one, String.t(), enforce: false field :field_two, integer() field :field_three, boolean() field :field_four, atom(), default: :hey end end ``` ```elixir defmodule MyModule do use TypedStruct typedstruct module: Struct do field :field_one, String.t() field :field_two, integer(), enforce: true field :field_three, boolean(), enforce: true field :field_four, atom(), default: :hey end end ``` -------------------------------- ### Fetch All Upstream Changes Source: https://hexdocs.pm/typedstruct/contributing Fetches all changes from all remotes and prunes stale branches, then rebases the current 'develop' branch onto the latest upstream 'develop'. This ensures the local branch is up-to-date. ```bash git checkout develop git fetch --all --prune git rebase upstream/developcopy ``` -------------------------------- ### Define an Empty TypedStruct Source: https://hexdocs.pm/typedstruct/readme This code shows how to define an empty `typedstruct` block. The result is an empty struct with its module type `t()`, demonstrating the minimal configuration required. ```Elixir defmodule Example do use TypedStruct typedstruct do end endcopy ``` -------------------------------- ### Extend TypedStruct with Plugins Source: https://hexdocs.pm/typedstruct/readme This snippet illustrates extending TypedStruct's functionality using its plugin interface. It demonstrates integrating the `TypedStructLens` plugin to automatically generate lenses for struct fields, enabling easy data modification. ```Elixir defmodule MyStruct do use TypedStruct typedstruct do plugin TypedStructLens field :a_field, String.t() field :other_field, atom() end @spec change(t()) :: t() def change(data) do # a_field/0 is generated by TypedStructLens. lens = a_field() put_in(data, [lens], "Changed") end endcopy ``` -------------------------------- ### Create New Feature Branch Source: https://hexdocs.pm/typedstruct/contributing Creates a new branch for development, typically for a small patch or a specific feature. The branch name should be explicit. ```bash git checkout -b copy ``` -------------------------------- ### Define Elixir Struct with Standard Syntax Source: https://hexdocs.pm/typedstruct/TypedStruct This snippet demonstrates the traditional Elixir way of defining a struct with explicit `@enforce_keys`, `defstruct`, and `@type` annotations. It requires duplicating key information across these definitions. ```elixir defmodule Person do @moduledoc """ A struct representing a person. """ @enforce_keys [:name] defstruct name: nil, age: nil, happy?: true, phone: nil @typedoc "A person" @type t() :: %__MODULE__{ name: String.t(), age: non_neg_integer() | nil, happy?: boolean(), phone: String.t() | nil } end ``` -------------------------------- ### Basic TypedStruct Definition in Elixir Source: https://hexdocs.pm/typedstruct/readme This Elixir code demonstrates the fundamental usage of TypedStruct for defining a struct with basic fields. It shows how to use `use TypedStruct` and the `typedstruct` block to declare fields with their types. ```elixir defmodule MyStruct do # Use TypedStruct to import the typedstruct macro. use TypedStruct # Define your struct. typedstruct do # Define each field with the field macro. field :a_string, String.t() # You can set a default value. field :string_with_default, String.t(), default: "default" # You can enforce a field. field :enforced_field, integer(), enforce: true end end ``` -------------------------------- ### Define Elixir Struct with TypedStruct Source: https://hexdocs.pm/typedstruct/readme This Elixir code snippet showcases the use of the TypedStruct library to define a struct with type information. It simplifies the definition by using `use TypedStruct` and a `typedstruct` block, where fields, types, and enforcement are declared more concisely. ```elixir defmodule Person do @moduledoc """ A struct representing a person. """ use TypedStruct typedstruct do @typedoc "A person" field :name, String.t(), enforce: true field :age, non_neg_integer() field :happy?, boolean(), default: true field :phone, String.t() end end ``` -------------------------------- ### Define Elixir Struct with TypedStruct Macro Source: https://hexdocs.pm/typedstruct/TypedStruct This snippet showcases how to define an Elixir struct using the TypedStruct library. It consolidates struct definition, type hinting, default values, and key enforcement into a single `typedstruct` block, significantly reducing boilerplate. ```elixir defmodule Person do @moduledoc """ A struct representing a person. """ use TypedStruct typedstruct do @typedoc "A person" field :name, String.t(), enforce: true field :age, non_neg_integer() field :happy?, boolean(), default: true field :phone, String.t() end end ``` -------------------------------- ### Add Documentation to TypedStruct Source: https://hexdocs.pm/typedstruct/TypedStruct Demonstrates how to add documentation to a TypedStruct type using the `@typedoc` attribute within the `typedstruct` block. This enhances the readability and maintainability of the struct definition. ```elixir typedstruct do @typedoc "A typed struct" field :a_string, String.t() field :an_int, integer() end ``` ```elixir typedstruct module: MyStruct do @moduledoc "A submodule with a typed struct." @typedoc "A typed struct in a submodule" field :a_string, String.t() field :an_int, integer() end ``` -------------------------------- ### Define an Empty TypedStruct Source: https://hexdocs.pm/typedstruct/TypedStruct Illustrates the definition of an empty TypedStruct, resulting in a basic Elixir struct with a `t()` type definition. This serves as a foundation for adding fields later. ```elixir defmodule Example do use TypedStruct typedstruct do end end ``` ```elixir defmodule Example do @enforce_keys [] defstruct [] @type t() :: %__MODULE__{} end ``` -------------------------------- ### Rebase Feature Branch onto Develop Source: https://hexdocs.pm/typedstruct/contributing Integrates changes from the 'develop' branch into the current feature branch by rebasing. This keeps the feature branch up-to-date with the latest development. ```bash git checkout git rebase developcopy ``` -------------------------------- ### Add @typedoc to Struct Type Source: https://hexdocs.pm/typedstruct/readme This snippet demonstrates how to add documentation to a struct type using the `@typedoc` attribute within a `typedstruct` block. It shows the basic syntax for documenting a struct with fields. ```Elixir typedstruct do @typedoc "A typed struct" field :a_string, String.t() field :an_int, integer() endcopy ``` -------------------------------- ### Add Upstream Remote Source: https://hexdocs.pm/typedstruct/contributing Adds the main TypedStruct repository as a remote named 'upstream'. This allows fetching changes from the original repository. ```bash git remote add upstream https://github.com/ejpcmac/typedstruct.gitcopy ``` -------------------------------- ### TypedStruct Plugin API: Add field/4 Callback (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Introduces the `field/4` callback in the TypedStruct plugin API, providing access to the environment during field definition. This replaces the older `field/3` callback. ```elixir # Example of implementing the new field/4 callback defmacro __using__(opts) do quote do # ... other definitions ... # New field callback signature @callback field(field_name :: atom(), type :: term(), default :: term(), env :: Keyword.t()) :: {:ok, term()} | {:error, term()} # Existing field/3 callback is now deprecated and will be removed in 1.0.0 @callback field(field_name :: atom(), type :: term(), default :: term()) :: {:ok, term()} | {:error, term()} @deprecated "Use field/4 instead." # ... rest of the macro ... end end # Example plugin implementation (simplified) defmodule MyPlugin do @behaviour TypedStruct.Plugin # Implement field/4 def field(field_name, type, default, env) do IO.puts("Field: #{inspect(field_name)}, Type: #{inspect(type)}, Default: #{inspect(default)}, Env: #{inspect(env)}") # ... logic to process field definition ... {:ok, :field_processed} end # Deprecated field/3 implementation (for backward compatibility) def field(field_name, type, default) do IO.puts("Deprecated field/3 called for: #{inspect(field_name)}") # Derive env or handle appropriately, potentially warning the user field(field_name, type, default, []) # Example: pass empty env end end ``` -------------------------------- ### Commit Changes Source: https://hexdocs.pm/typedstruct/contributing Commits staged changes with a message following the Conventional Commits format. Multiple commits can be made during development. ```bash # Some work git commit -am "feat: my first change" # Some work git commit -am "refactor: my second change" ... ``` -------------------------------- ### Register Plugin with TypedStruct Source: https://hexdocs.pm/typedstruct/TypedStruct Registers a custom plugin for the current struct definition within a TypedStruct block. It takes the plugin module and optional arguments. ```elixir typedstruct do plugin MyPlugin field :a_field, String.t() end ``` -------------------------------- ### Define a Typed Record Source: https://hexdocs.pm/typedstruct/readme This code defines a typed record using the `typedrecord` block. It specifies fields with types and default values, which are then translated into Elixir's `Record` macro definitions and type specifications. ```Elixir defmodule Person do use TypedStruct typedrecord :person do @typedoc "A person" field :name, String.t() field :age, non_neg_integer(), default: 0 end endcopy ``` -------------------------------- ### TypedStruct with Default Value Source: https://hexdocs.pm/typedstruct/readme This snippet demonstrates how to specify a default value for a struct field using the `default:` option. The provided default value is then used in the `defstruct` definition. ```Elixir field :name, String.t(), default: "John Smith" # Becomes defstruct name: "John Smith"copy ``` -------------------------------- ### Define TypedStruct with Default Field Value Source: https://hexdocs.pm/typedstruct/TypedStruct Illustrates how to set a default value for a field in a TypedStruct using the `default:` option. The specified default value is added to the `defstruct` definition. ```elixir field :name, String.t(), default: "John Smith" ``` ```elixir defstruct name: "John Smith" ``` -------------------------------- ### Define Elixir Record with TypedStruct Source: https://hexdocs.pm/typedstruct/readme This Elixir code snippet illustrates how to define an Erlang record using TypedStruct. The `typedrecord` block allows for a similar concise definition of fields and types as `typedstruct`, but for records. ```elixir defmodule Person do use TypedStruct typedrecord :person do @typedoc "A person" field :name, String.t(), field :age, non_neg_integer(), default: 0 end end ``` -------------------------------- ### Define TypedRecord with Fields Source: https://hexdocs.pm/typedstruct/TypedStruct Defines a typed record. Fields are defined using the `field/2` macro within the `typedrecord` block. Supports options like `tag`, `visibility`, and `module`. ```elixir # Example for typedrecord definition would go here, but is not provided in the input text. ``` -------------------------------- ### TypedStruct: Enforce Keys by Default (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Introduces the ability to enforce keys by default in TypedStruct definitions. This ensures that only defined keys are allowed when creating a struct. ```elixir defmodule MyEnforcedStruct do use TypedStruct, enforce_keys: true typedstruct do name: [type: String.t()] age: [type: integer(), default: 0] end end # Example usage: # Valid struct: # struct1 = %MyEnforcedStruct{name: "Alice", age: 30} # Invalid struct (will raise an error due to enforce_keys: true): # struct2 = %MyEnforcedStruct{name: "Bob", city: "New York"} # Error: unknown key :city # If enforce_keys is false or not specified, extra keys are allowed by default. ``` -------------------------------- ### TypedStruct with Enforced Keys by Default in Elixir Source: https://hexdocs.pm/typedstruct/readme This Elixir code shows how to configure TypedStruct to enforce all keys by default within a `typedstruct` block. It demonstrates overriding the default enforcement for specific fields. ```elixir defmodule MyStruct do use TypedStruct # Enforce keys by default. typedstruct enforce: true do # This key is enforced. field :enforced_by_default, term() # You can override the default behaviour. field :not_enforced, term(), enforce: false # A key with a default value is not enforced. field :not_enforced_either, integer(), default: 1 end end ``` -------------------------------- ### Define TypedStruct with Enforced Field Source: https://hexdocs.pm/typedstruct/TypedStruct Shows how to enforce a field in a TypedStruct using the `enforce: true` option. This adds the field to the `@enforce_keys` attribute, ensuring it must be provided during struct creation. ```elixir field :name, String.t(), enforce: true ``` ```elixir @enforce_keys [:name] defstruct name: nil ``` ```elixir @type t() :: %__MODULE__{ name: String.t() # Not nullable } ``` -------------------------------- ### Define Elixir Struct Manually Source: https://hexdocs.pm/typedstruct/readme This Elixir code snippet demonstrates the traditional way of defining a struct with enforced keys and type specifications. It requires explicit definition of struct fields, enforced keys, and type aliases, leading to repetitive code. ```elixir defmodule Person do @moduledoc """ A struct representing a person. """ @enforce_keys [:name] defstruct name: nil, age: nil, happy?: true, phone: nil @typedoc "A person" @type t() :: %__MODULE__{ name: String.t(), age: non_neg_integer() | nil, happy?: boolean(), phone: String.t() | nil } end ``` -------------------------------- ### Define TypedStruct within a Module Source: https://hexdocs.pm/typedstruct/TypedStruct Illustrates how to define a TypedStruct within a specific module using the `module:` option in the `typedstruct` block. This allows for nested module structures for the generated struct. ```elixir defmodule MyModule do use TypedStruct typedstruct module: Struct do field :field, term() end end ``` ```elixir defmodule MyModule do defmodule Struct do @enforce_keys [] defstruct field: nil @type t() :: %__MODULE__{ field: term() | nil } end end ``` -------------------------------- ### TypedStruct with Enforced Key Source: https://hexdocs.pm/typedstruct/readme This code shows how to enforce a struct key using the `enforce: true` option. This adds the field to the `@enforce_keys` attribute, making the field non-nullable in the generated type specification. ```Elixir field :name, String.t(), enforce: true # Becomes @enforce_keys [:name] defstruct name: nilcopy ``` -------------------------------- ### TypedStruct: Deprecate field/3 Callback (Elixir) Source: https://hexdocs.pm/typedstruct/changelog The `field/3` callback for TypedStruct plugins is deprecated in favor of `field/4`. Developers are encouraged to migrate by adding an `_env` argument to their implementations. ```elixir # In your plugin module: # Deprecated callback @callback field(field_name :: atom(), type :: term(), default :: term()) :: {:ok, term()} | {:error, term()} # Recommended new callback @callback field(field_name :: atom(), type :: term(), default :: term(), env :: Keyword.t()) :: {:ok, term()} | {:error, term()} # If implementing field/3 for backward compatibility: def field(field_name, type, default) do # Log a warning or handle the deprecation Logger.warn("field/3 callback is deprecated. Use field/4 instead.") # Call the new field/4 with a default or derived environment field(field_name, type, default, []) # Example: passing an empty environment end # Ensure you have the field/4 implementation as well def field(field_name, type, default, env) do # ... actual implementation using env ... {:ok, :processed} end ``` -------------------------------- ### TypedStruct: Enforced Keys Fix (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Addresses an issue where boolean fields with a default value of `false` were still enforced when `enforce: true` was set at the top level. ```elixir # This snippet illustrates a fix for a specific bug. # The actual code would be within the TypedStruct library itself. # Before the fix, a struct like this might have caused issues: defmodule BuggyStruct do use TypedStruct, enforce: true typedstruct do # This field might have been incorrectly enforced despite default: false enabled: [type: boolean(), default: false] end end # After the fix, creating %BuggyStruct{} should not raise an error related to the # 'enabled' field if it's not provided, because its default is false. # The fix likely involves adjusting the logic that checks for missing keys # to correctly handle fields with default values, especially booleans. ``` -------------------------------- ### TypedStruct Wrapped in a Module Source: https://hexdocs.pm/typedstruct/readme This snippet demonstrates how to define a TypedStruct within a nested module using the `module:` option. The entire `typedstruct` block is then wrapped inside the specified nested module definition. ```Elixir defmodule MyModule do use TypedStruct typedstruct module: Struct do field :field, term() end endcopy ``` -------------------------------- ### TypedStruct for Nested Structs in Elixir Source: https://hexdocs.pm/typedstruct/readme This Elixir code demonstrates how TypedStruct can be used to define a nested struct within a module. The `module: Struct` option in the `typedstruct` block allows for the creation of a named nested struct. ```elixir defmodule MyModule do use TypedStruct # You now have %MyModule.Struct{}. typedstruct module: Struct do field :field, term() end end ``` -------------------------------- ### TypedStruct: Opaque Type Generation (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Adds the capability to generate opaque types for TypedStruct definitions, enhancing type safety and encapsulation. ```elixir defmodule MyOpaqueStruct do use TypedStruct, generate_opaque_type: true typedstruct do field1: [type: integer(), default: 0] field2: [type: String.t(), default: "default"] end end # When generate_opaque_type is true, a type specification like # @type t :: #{field1 => integer(), field2 => String.t()} # will be generated, and the struct itself will be opaque. # You would typically interact with it via functions that accept/return MyOpaqueStruct.t() ``` -------------------------------- ### Define TypedStruct with Opaque Type Source: https://hexdocs.pm/typedstruct/TypedStruct Demonstrates using the `opaque: true` option within a `typedstruct` block. This replaces the `@type` annotation with `@opaque` for the struct's type specification, often used for hiding implementation details. ```elixir typedstruct opaque: true do field :name, String.t() end ``` ```elixir @opaque t() :: %__MODULE__{ name: String.t() } ``` -------------------------------- ### TypedStruct with Private Type Visibility in Elixir Source: https://hexdocs.pm/typedstruct/readme This Elixir code snippet illustrates how to generate a private type for a struct defined using TypedStruct. The `visibility: :private` option restricts the type's accessibility. ```elixir defmodule MyPrivateStruct do use TypedStruct # Generate a private type for the struct. typedstruct visibility: :private do field :name, String.t() end end ``` -------------------------------- ### TypedStruct: ModuleName Option for Submodule Structs (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Allows defining a TypedStruct within a submodule using the `module: ModuleName` option. This is particularly useful when defining submodules. ```elixir defmodule MyStruct do use TypedStruct typedstruct [module: MyStruct.Nested, version: "1.0.0"], field1: [type: integer(), default: 0], field2: [type: String.t(), default: "hello"] end # This will create a struct within MyStruct.Nested module # Example usage: # struct = %MyStruct.Nested{field1: 10, field2: "world"] # IO.inspect(struct.field1) # Output: 10 ``` -------------------------------- ### TypedStruct with Opaque Type Visibility in Elixir Source: https://hexdocs.pm/typedstruct/readme This Elixir code snippet demonstrates how to generate an opaque type for a struct defined using TypedStruct. The `visibility: :opaque` option ensures that the internal structure of the type is not exposed. ```elixir defmodule MyOpaqueStruct do use TypedStruct # Generate an opaque type for the struct. typedstruct visibility: :opaque do field :name, String.t() end end ``` -------------------------------- ### TypedStruct: Lexical Scope Fix in typestruct Block (Elixir) Source: https://hexdocs.pm/typedstruct/changelog Corrects the lexical scope within the `typedstruct` block, ensuring that definitions like aliases are available for field definitions. ```elixir defmodule ScopedStruct do use TypedStruct # Alias defined within the scope alias MyAlias.Thing typedstruct do # This field definition should now correctly resolve 'Thing' my_field: [type: Thing.t()] # Before the fix, 'Thing' might not have been defined in this scope. end end # Example of how 'Thing' might be defined elsewhere: # defmodule MyAlias do # defmodule Thing do # @type t :: :some_value # end # end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.