3 ms·
I'm just starting to learn Elixir and Phoenix. Here are two little bits of "magic" that have thrown me for a loop so far: 1. @var's in templates are not module
by phlakaton 11y ago
I'm just starting to learn Elixir and Phoenix. Here are two little bits of "magic" that have thrown me for a loop so far:
1. @var's in templates are not module attributes (though they do seem to be convenient!)
2. The form for `use` is different in Phoenix than it is in Elixir proper – instead of passing opts, you pass an atom that selects the implementation of __using__ you want. (This bit of magic makes no particular sense to me – why wouldn't you just use a submodule, say MyApp.Controller, that has the correct and single __using__ attached?)
- colbyh 11y agoyou can define `__using__` any way you want, and phoenix's default isn't technically any different. it's just a convention that you can choose to use or not, personally I try to rip out a lot of what the `web.ex` file does because I like being more explicit.
- true_religion 11y ago> The form for `use` is different in Phoenix than it is in Elixir proper – instead of passing opts, you pass an atom that selects the implementation of __using__ you want. This isn't really true. If you check out the ExUnit, you can pass something like "use ExUnit.Case, aysnc: true" and it'll setup your module to run tests asynchronously. You can pass any atom/value combination into use. HTTPoision.Base, and ExAws to name a few other projects also have the convention of passing options into __using__. To me, the confusion comes from the fact that the Elixir get-started documentation doesn't say anything about this. It is however listed in the Kernel forms that "use Module" actually just translates to Module.__using__([])
- phlakaton 10y agoThat's the point though - in those cases, you're passing a keyword list, a set of options, instead of an atom which acts as a selector. IIUC the Web module violates the convention attached to and documented for `use`, which to me, who was already wrangling with the subtleties of what `use` does compared to, say, Ruby's include, or Elixir's alias/import, was a bit of confusing (and relatively unconventional) magic.