@@ -766,15 +766,9 @@ defmodule Module do
766
766
767
767
@foo
768
768
769
- Expands to:
769
+ Expands close to:
770
770
771
- Module.get_attribute(__MODULE__, :foo, [{Module, :func, 0, [file: "module.ex", line: 1]}])
772
-
773
- Notice the third argument may be given to indicate a stacktrace
774
- to be emitted when the attribute was not previously defined.
775
- The default value for `warn` is nil for direct calls but the `@foo`
776
- macro sets it to the proper stacktrace automatically, warning
777
- every time `@foo` is used but not set previously.
771
+ Module.get_attribute(__MODULE__, :foo)
778
772
779
773
## Examples
780
774
@@ -788,37 +782,9 @@ defmodule Module do
788
782
end
789
783
790
784
"""
791
- @ spec get_attribute ( module , atom , warn :: nil | [ tuple ] ) :: term
792
- def get_attribute ( module , key , warn \\ nil ) when
793
- is_atom ( key ) and ( is_list ( warn ) or nil? ( warn ) ) do
794
- assert_not_compiled! ( :get_attribute , module )
795
- table = data_table_for ( module )
796
-
797
- case :ets . lookup ( table , key ) do
798
- [ { ^ key , val } ] -> val
799
- [ ] ->
800
- acc = :ets . lookup_element ( table , :__acc_attributes , 2 )
801
-
802
- cond do
803
- :lists . member ( key , acc ) ->
804
- [ ]
805
- is_list ( warn ) ->
806
- :elixir_errors . warn warn_info ( warn ) , "undefined module attribute @#{ key } , " <>
807
- "please remove access to @#{ key } or explicitly set it to nil before access"
808
- nil
809
- true ->
810
- nil
811
- end
812
- end
813
- end
814
-
815
- defp warn_info ( [ entry | _ ] ) do
816
- opts = elem ( entry , tuple_size ( entry ) - 1 )
817
- Exception . format_file_line ( Keyword . get ( opts , :file ) , Keyword . get ( opts , :line ) ) <> " "
818
- end
819
-
820
- defp warn_info ( [ ] ) do
821
- ""
785
+ @ spec get_attribute ( atom , atom ) :: term
786
+ def get_attribute ( module , key ) do
787
+ get_attribute ( module , key , nil )
822
788
end
823
789
824
790
@ doc """
@@ -832,10 +798,12 @@ defmodule Module do
832
798
end
833
799
834
800
"""
801
+ @ spec delete_attribute ( atom , atom ) :: :ok
835
802
def delete_attribute ( module , key ) when is_atom ( key ) do
836
803
assert_not_compiled! ( :delete_attribute , module )
837
804
table = data_table_for ( module )
838
805
:ets . delete ( table , key )
806
+ :ok
839
807
end
840
808
841
809
@ doc """
@@ -935,6 +903,38 @@ defmodule Module do
935
903
:ets . insert ( table , { key , new } )
936
904
end
937
905
906
+ @ doc false
907
+ def get_attribute ( module , key , warn ) when is_atom ( key ) and ( is_list ( warn ) or nil? ( warn ) ) do
908
+ assert_not_compiled! ( :get_attribute , module )
909
+ table = data_table_for ( module )
910
+
911
+ case :ets . lookup ( table , key ) do
912
+ [ { ^ key , val } ] -> val
913
+ [ ] ->
914
+ acc = :ets . lookup_element ( table , :__acc_attributes , 2 )
915
+
916
+ cond do
917
+ :lists . member ( key , acc ) ->
918
+ [ ]
919
+ is_list ( warn ) ->
920
+ :elixir_errors . warn warn_info ( warn ) , "undefined module attribute @#{ key } , " <>
921
+ "please remove access to @#{ key } or explicitly set it to nil before access"
922
+ nil
923
+ true ->
924
+ nil
925
+ end
926
+ end
927
+ end
928
+
929
+ defp warn_info ( [ entry | _ ] ) do
930
+ opts = elem ( entry , tuple_size ( entry ) - 1 )
931
+ Exception . format_file_line ( Keyword . get ( opts , :file ) , Keyword . get ( opts , :line ) ) <> " "
932
+ end
933
+
934
+ defp warn_info ( [ ] ) do
935
+ ""
936
+ end
937
+
938
938
## Helpers
939
939
940
940
defp normalize_attribute ( :on_load , atom ) when is_atom ( atom ) do
0 commit comments