@@ -3112,22 +3112,6 @@ defmodule Kernel do
3112
3112
3113
3113
user.update_age(fn(old) -> old + 1 end)
3114
3114
3115
- ## Types
3116
-
3117
- Every record defines a type named `t` that can be accessed in typespecs.
3118
- Those types can be specified inside the record definition:
3119
-
3120
- defrecord User do
3121
- record_type name: string, age: integer
3122
- end
3123
-
3124
- All fields without a specified type are assumed to have type `term`.
3125
-
3126
- Assuming the `User` record defined above, it could be used in typespecs
3127
- as follow:
3128
-
3129
- @spec handle_user(User.t) :: boolean()
3130
-
3131
3115
## Runtime introspection
3132
3116
3133
3117
At runtime, developers can use `__record__` to get information
@@ -3148,31 +3132,6 @@ defmodule Kernel do
3148
3132
User.__record__(:index, :unknown)
3149
3133
#=> nil
3150
3134
3151
- ## Compile-time introspection
3152
-
3153
- At compile time, one can access the following information about the record
3154
- from within the record module:
3155
-
3156
- * `@record_fields` — a keyword list of record fields with defaults
3157
- * `@record_types` — a keyword list of record fields with types
3158
-
3159
- For example:
3160
-
3161
- defrecord Foo, bar: nil do
3162
- record_type bar: nil | integer
3163
- IO.inspect @record_fields
3164
- IO.inspect @record_types
3165
- end
3166
-
3167
- Prints out:
3168
-
3169
- [bar: nil]
3170
- [bar: {:|,[line: ...],[nil,{:integer,[line: ...],nil}]}]
3171
-
3172
- Where the last line is a quoted representation of
3173
-
3174
- [bar: nil | integer]
3175
-
3176
3135
"""
3177
3136
defmacro defrecord ( name , fields , do_block \\ [ ] ) do
3178
3137
case is_list ( fields ) and Keyword . get ( fields , :do , false ) do
@@ -3226,20 +3185,6 @@ defmodule Kernel do
3226
3185
3227
3186
state() #=> { MyServer, nil }
3228
3187
3229
- ## Types
3230
-
3231
- `defrecordp` allows a developer to generate a type
3232
- automatically by simply providing a type to its fields.
3233
- The following definition:
3234
-
3235
- defrecordp :user,
3236
- name: "José" :: binary,
3237
- age: 25 :: integer
3238
-
3239
- Will generate the following type:
3240
-
3241
- @typep user_t :: { :user, binary, integer }
3242
-
3243
3188
"""
3244
3189
defmacro defrecordp ( name , tag \\ nil , fields ) do
3245
3190
Record.Deprecated . defrecordp ( name , Macro . expand ( tag , __CALLER__ ) , fields )
@@ -3254,8 +3199,8 @@ defmodule Kernel do
3254
3199
3255
3200
To define a struct, a developer needs to only define
3256
3201
a function named `__struct__/0` that returns a map with the
3257
- structs field. This macro is a convenience for doing such
3258
- function and a type `t` in one pass .
3202
+ structs field. This macro is a convenience for defining such
3203
+ function, with the addition of a type `t`.
3259
3204
3260
3205
For more information about structs, please check
3261
3206
`Kernel.SpecialForms.%/2`.
@@ -3282,19 +3227,32 @@ defmodule Kernel do
3282
3227
defstruct my_fields
3283
3228
end
3284
3229
3230
+ ## Types
3231
+
3232
+ `defstruct` automatically generates a type `t` unless one exists.
3233
+ The following definition:
3234
+
3235
+ defmodule User do
3236
+ defstruct name: "José" :: String.t,
3237
+ age: 25 :: integer
3238
+ end
3239
+
3240
+ Generates a type as follows:
3241
+
3242
+ @type t :: %User{name: String.t, age: integer}
3243
+
3244
+ In case a struct does not declare a field type, it defaults to `term`.
3285
3245
"""
3286
3246
defmacro defstruct ( kv ) do
3287
3247
kv = Macro . escape ( kv , unquote: true )
3288
3248
quote bind_quoted: [ kv: kv ] do
3289
3249
# Expand possible macros that return KVs.
3290
3250
kv = Macro . expand ( kv , __ENV__ )
3291
-
3292
- # TODO: Use those types once we support maps typespecs.
3293
- { fields , _types } = Record.Backend . split_fields_and_types ( :defstruct , kv )
3251
+ { fields , types } = Record.Backend . split_fields_and_types ( :defstruct , kv )
3294
3252
3295
3253
if :code . ensure_loaded ( Kernel.Typespec ) == { :module , Kernel.Typespec } and
3296
3254
not Kernel.Typespec . defines_type? ( __MODULE__ , :t , 0 ) do
3297
- @ type t :: map
3255
+ @ type t :: % { unquote_splicing ( types ) , __struct__: __MODULE__ }
3298
3256
end
3299
3257
3300
3258
def __struct__ ( ) do
0 commit comments