{"id":313021,"date":"2020-11-12T09:01:37","date_gmt":"2020-11-12T09:01:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=313021"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=313021","title":{"rendered":"\u0422\u0438\u043f\u044b, \u0433\u0434\u0435 \u0438\u0445 \u043d\u0435 \u0436\u0434\u0430\u043b\u0438"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u043e\u0434\u0443\u043b\u044f <code>Scaffold<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u043f\u043e\u043b\u044f\u043c\u0438 \u0438 \u0438\u043d\u0436\u0435\u043a\u0442\u0438\u0442 \u0435\u0435 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>use Scaffold<\/code>. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>use Scaffold, fields: foo: [custom_type()], ...<\/code>\u00a0\u2014 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f \u0432 <code>Consumer<\/code> \u043c\u043e\u0434\u0443\u043b\u0435 (<code>common_field<\/code> \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0432 <code>Scaffold<\/code> \u0438\u043b\u0438 \u0435\u0449\u0435 \u0433\u0434\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0438\u0437\u0432\u043d\u0435).<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">@type t :: %Consumer{   common_field: [atom()],   foo: [custom_type()],   ... }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0411\u044b\u043b\u043e \u0431\u044b \u043a\u0440\u0443\u0442\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0442\u043e\u0447\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0438\u043f <code>Consumer.t()<\/code> \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430\u0448\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/zy\/46\/4x\/zy464xmqifsrwocbtu1rynjdgrk.jpeg\" alt=\"Lighthouse in French Catalonia\"><\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmodule Scaffold do   defmacro __using__(opts) do     quote do       @fields unquote(opts[:fields])       @type t :: %__MODULE__{         version: atom()         # magic       }       defstruct @fields     end   end end  defmodule Consumer do   use Scaffold, fields: [foo: integer(), bar: binary()] end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438, \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmodule Consumer do   @type t :: %Consumer{     version: atom(),     foo: integer(),     bar: binary()   }   defstruct ~w|version foo bar|a end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e, \u0434\u0430?<\/p>\n<p>  <\/p>\n<h3 id=\"naivnyy-podhod\">\u041d\u0430\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434<\/h3>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0437\u0430 AST \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 <code>Scaffold.__using__\/1<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">  defmacro __using__(opts) do     IO.inspect(opts)   end #\u21d2 [fields: [foo: {:integer, [line: 2], []}, #            bar: {:binary, [line: 2], []}]]<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a, \u043a\u0430\u043a \u0431\u0443\u0434\u0442\u043e \u043c\u044b \u0432 \u0448\u0430\u0433\u0435 \u043e\u0442 \u0443\u0441\u043f\u0435\u0445\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">  quote do     custom_types = unquote(opts[:fields])     ...   end #\u21d2 == Compilation error in file lib\/consumer.ex == #  ** (CompileError) lib\/consumer.ex:2: undefined function integer\/0<\/code><\/pre>\n<p>  <\/p>\n<p>\u0411\u0430\u043c\u0441! \u0422\u0438\u043f\u044b\u00a0\u2014\u00a0\u044d\u0442\u043e \u0447\u0435\u0433\u043e-\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e, \u043a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u0432 \u0440\u0430\u0439\u043e\u043d\u0435 \u041f\u0440\u0438\u0432\u043e\u0437\u0430; \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438\u0445 \u0438\u0437 AST \u0433\u0434\u0435 \u043f\u043e\u043f\u0430\u043b\u043e. \u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, <code>unquote<\/code> \u043f\u043e \u043c\u0435\u0441\u0442\u0443 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">      @type t :: %__MODULE__{               unquote_splicing([{:version, atom()} | opts[:fields]])             } #\u21d2 == Compilation error in file lib\/scaffold.ex == #  ** (CompileError) lib\/scaffold.ex:11: undefined function atom\/0<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0431\u044b \u043d\u0435 \u0442\u0430\u043a. \u0422\u0438\u043f\u044b\u00a0\u2014\u00a0\u044d\u0442\u043e \u0443\u0442\u043e\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u043e; \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u043b\u044e\u0431\u043e\u0433\u043e, \u043a\u0442\u043e \u0437\u0430\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0436\u0438\u0437\u043d\u044c \u0445\u0430\u0441\u043a\u0435\u043b\u0435\u043c (\u0438 \u044d\u0442\u043e \u0435\u0449\u0435 \u0432 \u0445\u0430\u0441\u043a\u0435\u043b\u0435 \u0442\u0438\u043f\u044b \u043a\u0443\u0440\u0438\u043b\u044c\u0449\u0438\u043a\u0430; \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0435 \u2014\u00a0\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0435\u00a0\u2014 \u0442\u0438\u043f\u044b \u0432 \u0441\u0442\u043e \u0440\u0430\u0437 \u043f\u043e\u043b\u0435\u0437\u043d\u0435\u0435, \u043d\u043e \u0435\u0449\u0435 \u0432 \u0434\u0432\u0435\u0441\u0442\u0438 \u0440\u0430\u0437 \u0441\u043b\u043e\u0436\u043d\u0435\u0435).<\/p>\n<p>  <\/p>\n<p>\u041b\u0430\u0434\u043d\u043e, \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u043e\u0433\u0430\u0442\u0441\u0442\u0432\u043e \u0432 AST \u0438 \u0438\u043d\u0436\u0435\u043a\u0442\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0446\u0435\u043b\u0438\u043a\u043e\u043c, \u0430 \u043d\u0435 \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0443\u0432\u0438\u0434\u0435\u043b \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<h3 id=\"postroenie-tipa-v-ast\">\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 \u0432 AST<\/h3>\n<p>  <\/p>\n<p>\u042f \u043e\u043f\u0443\u0449\u0443 \u0442\u0443\u0442 \u043f\u0435\u0440\u0435\u0441\u043a\u0430\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0447\u0430\u0441\u043e\u0432 \u043c\u043e\u0438\u0445 \u043c\u0435\u0442\u0430\u043d\u0438\u0439, \u043c\u0443\u0447\u0435\u043d\u0438\u0439, \u0438 \u0442\u044b\u0447\u043a\u043e\u0432 \u043f\u0430\u043b\u044c\u0446\u0435\u043c \u0432 \u043d\u0435\u0431\u043e. \u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u0447\u0442\u043e \u044f \u043f\u0438\u0448\u0443 \u043a\u043e\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043d\u0430\u0443\u0433\u0430\u0434, \u043e\u0436\u0438\u0434\u0430\u044f, \u0447\u0442\u043e \u0432\u0434\u0440\u0443\u0433 \u043a\u0430\u043a\u0430\u044f-\u043d\u0438\u0431\u0443\u0434\u044c \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u043e\u043a \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0412 \u043e\u0431\u0449\u0435\u043c, \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0442\u0443\u0442 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c. \u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u043f\u0438\u0445\u043d\u0443\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432 \u043c\u0430\u043a\u0440\u043e\u0441, \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0442\u0438\u043f, \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043f\u043e\u043f\u044b\u0442\u0430\u0432\u0448\u0438\u0441\u044c \u044d\u0442\u043e AST \u0430\u043d\u043a\u0432\u043e\u0442\u043d\u0443\u0442\u044c (\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 <code>unquote<\/code> \u0442\u0438\u043f\u044b \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 <code>binary()<\/code> \u0431\u0443\u0434\u0443\u0442 \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u044b \u0437\u0430 \u043e\u0431\u044b\u043a\u043d\u043e\u0432\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 <del>\u0443\u0431\u0438\u0442\u044b \u0438\u0437 \u0431\u0430\u0437\u0443\u043a\u0438<\/del> \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043f\u0440\u0438\u0432\u043e\u0434\u044f \u043a <code>CompileError<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <em>\u0432\u043d\u0443\u0442\u0440\u0438<\/em> <code>quote do<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0431\u043b\u043e\u043a\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432 <code>quote<\/code>, \u0443\u0436\u0435 \u0441\u0430\u043c\u043e \u043f\u043e \u0441\u0435\u0431\u0435\u00a0\u2014\u00a0AST.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">quote do   Enum.map([:foo, :bar], &amp; &amp;1) end #\u21d2 { #   {:., [], [{:__aliases__, [alias: false], [:Enum]}, :map]}, [], #     [[:foo, :bar], {:&amp;, [], [{:&amp;, [], [1]}]}]}<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0438\u0434\u0438\u0442\u0435? \u0412\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0435\u0435 \u043f\u0440\u0435\u043f\u0430\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 AST, \u0432\u0441\u0435 \u044d\u0442\u0438 <code>Enum<\/code>, <code>:map<\/code>, \u0438 \u043f\u0440\u043e\u0447\u0438\u0439 \u043c\u0430\u043b\u043e\u0432\u043d\u044f\u0442\u043d\u044b\u0439 \u043c\u0443\u0441\u043e\u0440. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c AST \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u0430 \u0432\u043d\u0435 \u0431\u043b\u043e\u043a\u0430 <code>quote<\/code> \u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0430\u043d\u043a\u0432\u043e\u0442\u043d\u0443\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0433\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c.<\/p>\n<p>  <\/p>\n<h3 id=\"chut-menee-naivnaya-popytka\">\u0427\u0443\u0442\u044c \u043c\u0435\u043d\u0435\u0435 \u043d\u0430\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430<\/h3>\n<p>  <\/p>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u043c \u043d\u0430\u0434\u043e <em>\u0438\u043d\u0436\u0435\u043a\u0442\u043d\u0443\u0442\u044c<\/em> AST \u043a\u0430\u043a AST, \u043d\u0435 \u043f\u044b\u0442\u0430\u044f\u0441\u044c \u0435\u0433\u043e \u0430\u043d\u043a\u0432\u043e\u0442\u043d\u0443\u0442\u044c. \u0417\u0432\u0443\u0447\u0438\u0442 \u0443\u0441\u0442\u0440\u0430\u0448\u0430\u044e\u0449\u0435? \u00a0\u2014\u00a0\u0412\u043e\u0432\u0441\u0435 \u043d\u0435\u0442, \u043e\u0442\u043d\u044e\u0434\u044c.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmacro __using__(opts) do   fields = opts[:fields]   keys = Keyword.keys(fields)   type = ???    quote location: :keep do     @type t :: unquote(type)     defstruct unquote(keys)   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0435\u0439\u0447\u0430\u0441,\u00a0\u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0449\u0438\u0439 AST, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u041d\u0443, \u043f\u0443\u0441\u0442\u044c <em>ruby<\/em> \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e \u0437\u0430 \u043d\u0430\u0441!<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">iex|1  quote do ...|1    %Foo{version: atom(), foo: binary()} ...|1  end #\u21d2\u00a0{:%, [], #   [ #     {:__aliases__, [alias: false], [:Foo]}, #     {:%{}, [], [version: {:atom, [], []}, foo: {:binary, [], []}]} #   ]}<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u043d\u0435\u043b\u044c\u0437\u044f \u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0449\u0435?<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">iex|2  quote do ...|2    %{__struct__: Foo, version: atom(), foo: binary()} ...|2  end #\u21d2 {:%{}, [], #   [ #     __struct__: {:__aliases__, [alias: false], [:Foo]}, #     version: {:atom, [], []}, #     foo: {:binary, [], []} #   ]}<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0443, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044e\u0449\u0435 \u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e\u043e\u0431\u0435\u0449\u0430\u044e\u0449\u0435. \u041f\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0440\u0430\u0431\u043e\u0447\u0435\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<p>  <\/p>\n<h3 id=\"pochti-rabotayuschee-reshenie\">\u041f\u043e\u0447\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmacro __using__(opts) do   fields = opts[:fields]   keys = Keyword.keys(fields)   type =     {:%{}, [],       [         {:__struct__, {:__MODULE__, [], ruby}},         {:version, {:atom, [], []}}         | fields       ]}    quote location: :keep do     @type t :: unquote(type)     defstruct unquote(keys)   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438\u043b\u0438, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u0446\u0435\u043b\u0438 \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0442\u0438\u043f\u044b \u0438\u0437 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e <code>Scaffold<\/code>, \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0449\u0435 (\u043a\u0430\u043a \u043c\u043d\u0435 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u0430\u043b\u0438: <a href=\"https:\/\/rubyforum.com\/t\/how-to-generate-custom-types-with-macros\/33122\/4?u=mudasobwa\" rel=\"nofollow\"><em>Qqwy<\/em> here<\/a>). \u041e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e, \u043e\u043d\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043f\u0440\u043e\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438, <code>version: atom()<\/code> \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0431\u043b\u043e\u043a\u0430 <code>quote<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmacro __using__(opts) do   fields = opts[:fields]   keys = Keyword.keys(fields)   fields_with_struct_name = [__struct__: __CALLER__.module] ++ fields    quote location: :keep do     @type t :: %{unquote_splicing(fields_with_struct)}     defstruct unquote(keys)   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f (<code>mix docs<\/code>):<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/3s\/yl\/3n\/3syl3nqdijehmt3j0zxmovvrjto.png\" alt=\"Screenshot of type definition\"><\/p>\n<p>  <\/p>\n<h3 id=\"primechanie-tryuk-s-fragmentom-ast\">\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0442\u0440\u044e\u043a \u0441 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u043c AST<\/h3>\n<p>  <\/p>\n<p>\u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0431\u043b\u043e\u043a AST \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e <code>__using__\/1<\/code> \u043c\u0430\u043a\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043a\u0430\u0432\u044b\u0447\u043a\u0430\u0445? \u041f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u043e\u043d\u043d\u0443 \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0447\u0435\u0440\u0435\u0434\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <code>unquote<\/code> \u0438\u0437\u043d\u0443\u0442\u0440\u0438 <code>quote<\/code>? \u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0430\u0436\u0435 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u043c\u0435\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u043e \u0432\u0441\u0435\u043c\u0443, \u0447\u0442\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f. \u041d\u0430 \u043d\u0430\u0448\u0435 \u0441\u0447\u0430\u0441\u0442\u044c\u0435, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043f\u0440\u043e\u0449\u0435.<\/p>\n<p>  <\/p>\n<blockquote><p><strong>NB<\/strong> \u0434\u043b\u044f \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u0438 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u043f\u043e\u043b\u0435\u0439, \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0442\u0438\u043f <code>atom()<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u044c\u0441\u044f \u0434\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044f \u043b\u044e\u0431\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0432\u043d\u0435\u0448\u043d\u0438\u0435, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a <code>GenServer.on_start()<\/code> \u0438 \u0435\u043c\u0443 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435. \u042d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0434\u043b\u044f \u044d\u043d\u0442\u0443\u0437\u0438\u0430\u0441\u0442\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 \u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f.<\/p><\/blockquote>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0438\u043f <em>\u0432\u043d\u0443\u0442\u0440\u0438<\/em> \u0431\u043b\u043e\u043a\u0430 <code>quote do<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0443\u0434\u0430-\u0441\u044e\u0434\u0430 <code>atom()<\/code> (\u043e\u043d\u043e \u0432\u0437\u043e\u0432\u0435\u0442\u0441\u044f \u0441 <code>CompileError<\/code>, \u043a\u0430\u043a \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0432\u044b\u0448\u0435). \u0425\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0438\u043f\u0430 \u0442\u0430\u043a\u043e\u0433\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">keys = Keyword.keys(fields) type =   {:%{}, [],     [       {:__struct__, {:__MODULE__, [], ruby}},       {:version, {:atom, [], []}}       | Enum.zip(keys, Stream.cycle([{:atom, [], []}]))     ]}<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u043a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0410\u0421\u0422 \u0432 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0446\u0438\u044e <code>@type<\/code>? \u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u044d\u043b\u0438\u043a\u0441\u0438\u0440\u0430 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <a href=\"https:\/\/hexdocs.pm\/ruby\/Kernel.SpecialForms.html#quote\/2-binding-and-unquote-fragments\" rel=\"nofollow\">Quoted Fragment<\/a>, \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432 \u044f\u0437\u044b\u043a \u0440\u0430\u0434\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438\u044e \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmodule Squares do   Enum.each(1..42, fn i -&gt;     def unquote(:&quot;squared_#{i}&quot;)(),       do: unquote(i) * unquote(i)   end) end Squares.squared_5 #\u21d2 25<\/code><\/pre>\n<p>  <\/p>\n<p><em>Quoted Fragments<\/em> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u044e\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u043e\u0432 <code>quote<\/code>, \u0441 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c (<code>bind_quoted:<\/code>). \u041f\u0440\u043e\u0449\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">defmacro __using__(opts) do   keys = Keyword.keys(opts[:fields])    quote location: :keep, bind_quoted: [keys: keys] do     type =       {:%{}, [],         [           {:__struct__, {:__MODULE__, [], ruby}},           {:version, {:atom, [], []}}           | Enum.zip(keys, Stream.cycle([{:atom, [], []}]))         ]}      #          \u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3\u21d3     @type t :: unquote(type)     defstruct keys   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0434\u0438\u043d\u043e\u043a\u0438\u0439 \u0432\u044b\u0437\u043e\u0432 <code>unquote\/1<\/code> \u0442\u0443\u0442 <em>\u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d<\/em>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>bind_quoted:<\/code> \u0431\u044b\u043b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u043a\u0430\u0437\u0430\u043d \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0432 \u0432\u044b\u0437\u043e\u0432\u0435 <code>quote\/2<\/code>.<\/p>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<p>\u0423\u0434\u0430\u0447\u043d\u043e\u0433\u043e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f!<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/527646\/\"> https:\/\/habr.com\/ru\/post\/527646\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u043e\u0434\u0443\u043b\u044f <code>Scaffold<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u043f\u043e\u043b\u044f\u043c\u0438 \u0438 \u0438\u043d\u0436\u0435\u043a\u0442\u0438\u0442 \u0435\u0435 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>use Scaffold<\/code>. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>use Scaffold, fields: foo: [custom_type()], ...<\/code>\u00a0\u2014 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f \u0432 <code>Consumer<\/code> \u043c\u043e\u0434\u0443\u043b\u0435 (<code>common_field<\/code> \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0432 <code>Scaffold<\/code> \u0438\u043b\u0438 \u0435\u0449\u0435 \u0433\u0434\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0438\u0437\u0432\u043d\u0435).<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">@type t :: %Consumer{   common_field: [atom()],   foo: [custom_type()],   ... }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0411\u044b\u043b\u043e \u0431\u044b \u043a\u0440\u0443\u0442\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0442\u043e\u0447\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0438\u043f <code>Consumer.t()<\/code> \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430\u0448\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/zy\/46\/4x\/zy464xmqifsrwocbtu1rynjdgrk.jpeg\" alt=\"Lighthouse in French Catalonia\"><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-313021","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313021"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313021\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}