@@ -3,6 +3,9 @@ import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
33defmodule Kernel.Utils do
44 @ moduledoc false
55
6+ @ doc """
7+ Callback for destructure.
8+ """
69 def destructure ( list , count ) when is_list ( list ) , do: destructure_list ( list , count )
710 def destructure ( nil , count ) , do: destructure_nil ( count )
811
@@ -13,6 +16,9 @@ defmodule Kernel.Utils do
1316 defp destructure_nil ( 0 ) , do: [ ]
1417 defp destructure_nil ( count ) , do: [ nil | destructure_nil ( count - 1 ) ]
1518
19+ @ doc """
20+ Callback for defdelegate.
21+ """
1622 def defdelegate ( fun , opts ) do
1723 append_first = Keyword . get ( opts , :append_first , false )
1824
@@ -56,6 +62,9 @@ defmodule Kernel.Utils do
5662 "defdelegate/2 only accepts function parameters, got: #{ Macro . to_string ( code ) } "
5763 end
5864
65+ @ doc """
66+ Callback for defstruct.
67+ """
5968 def defstruct ( module , fields ) do
6069 case fields do
6170 fs when is_list ( fs ) ->
@@ -85,10 +94,30 @@ defmodule Kernel.Utils do
8594 Module . get_attribute ( module , :derive ) }
8695 end
8796
97+ @ doc """
98+ Announcing callback for defstruct.
99+ """
88100 def announce_struct ( module ) do
89101 case :erlang . get ( :elixir_compiler_pid ) do
90102 :undefined -> :ok
91103 pid -> send ( pid , { :struct_available , module } )
92104 end
93105 end
106+
107+ @ doc """
108+ Callback for raise.
109+ """
110+ def raise ( msg ) when is_binary ( msg ) do
111+ RuntimeError . exception ( msg )
112+ end
113+ def raise ( atom ) when is_atom ( atom ) do
114+ atom . exception ( [ ] )
115+ end
116+ def raise ( % { __struct__: struct , __exception__: true } = exception ) when is_atom ( struct ) do
117+ exception
118+ end
119+ def raise ( other ) do
120+ ArgumentError . exception ( "raise/1 expects an alias, string or exception as " <>
121+ "the first argument, got: #{ inspect other } " )
122+ end
94123end
0 commit comments