@@ -3,6 +3,9 @@ import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
3
3
defmodule Kernel.Utils do
4
4
@ moduledoc false
5
5
6
+ @ doc """
7
+ Callback for destructure.
8
+ """
6
9
def destructure ( list , count ) when is_list ( list ) , do: destructure_list ( list , count )
7
10
def destructure ( nil , count ) , do: destructure_nil ( count )
8
11
@@ -13,6 +16,9 @@ defmodule Kernel.Utils do
13
16
defp destructure_nil ( 0 ) , do: [ ]
14
17
defp destructure_nil ( count ) , do: [ nil | destructure_nil ( count - 1 ) ]
15
18
19
+ @ doc """
20
+ Callback for defdelegate.
21
+ """
16
22
def defdelegate ( fun , opts ) do
17
23
append_first = Keyword . get ( opts , :append_first , false )
18
24
@@ -56,6 +62,9 @@ defmodule Kernel.Utils do
56
62
"defdelegate/2 only accepts function parameters, got: #{ Macro . to_string ( code ) } "
57
63
end
58
64
65
+ @ doc """
66
+ Callback for defstruct.
67
+ """
59
68
def defstruct ( module , fields ) do
60
69
case fields do
61
70
fs when is_list ( fs ) ->
@@ -85,10 +94,30 @@ defmodule Kernel.Utils do
85
94
Module . get_attribute ( module , :derive ) }
86
95
end
87
96
97
+ @ doc """
98
+ Announcing callback for defstruct.
99
+ """
88
100
def announce_struct ( module ) do
89
101
case :erlang . get ( :elixir_compiler_pid ) do
90
102
:undefined -> :ok
91
103
pid -> send ( pid , { :struct_available , module } )
92
104
end
93
105
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
94
123
end
0 commit comments