Neither `%_{}` nor `%self{}` can be supported because they already have a meaning in pattern matches. The former means _any_ struct and the latter binds the matched struct name to the variable `self`.
You can give `__MODULE__` another name with an alias: alias __MODULE__, as: Struct def connect(%Struct{} = channel) On January 21, 2022, "gmail.com" <inss...@gmail.com> wrote: > It is common to define a struct together with various functions that > access that struct in the module: > > defmodule Chat.Channel do > defstruct name: "", public?: true > > def new do > %Chat.Channel{name: "Untitled"} > end > > def connect(%Chat.Channel{} = channel) do > IO.inspect(channel) > end > end > > It is also common to alias the struct for easier access > > defmodule Chat.Channel do > defstruct name: "", public?: true > > alias Chat.Channel > > # ... > end > > But, say, renaming the module would require manually replacing all > struct occurrences with the new module name. Aliasing can help, but if > the last bit should be updated too(say Chat.Channel should be updated > to Chat.Room) it would still require to manually replace everything. > > There is a workaround to use __MODULE__, but IMO the code looks a bit > ugly > > defmodule Chat.Channel do > defstruct name: "", public?: true > > def new do > %__MODEUL__{name: "Untitled"} > end > > def connect(%__MODEUL__{} = channel) do > IO.inspect(channel) > end > end > > I think It would be great to have some kind of shortcut(syntactic > sugar) to access the struct within the module. > First I thought about something like %_(%%, %. etc) but this way it > looks a bit cryptic > > def connect(%_{} = channel) do > > So maybe something like %self would work > > defmodule Chat.Channel do > defstruct name: "", public?: true > > def new do > %self{name: "Untitled"} > end > > def connect(%self{} = channel) do > IO.inspect(channel) > end > end > > What do you think? > -- > You received this message because you are subscribed to the Google > Groups "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, > send an email to elixir-lang-core+unsubscr...@googlegroups.com > <mailto:elixir-lang-core+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/da49bf41-d4ad-4fc7- > a88c-1338e7a463c1n%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/da49bf41-d4ad- > 4fc7-a88c- > 1338e7a463c1n%40googlegroups.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/38c71ced95b2fe910b644fda40da8cb645914f3f%40hey.com.