File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -1673,7 +1673,8 @@ A ``distinct`` type is new type derived from a `base type`:idx: that is
1673
1673
incompatible with its base type. In particular, it is an essential property
1674
1674
of a distinct type that it **does not ** imply a subtype relation between it
1675
1675
and its base type. Explicit type conversions from a distinct type to its
1676
- base type and vice versa are allowed.
1676
+ base type and vice versa are allowed. See also ``distinctBase `` to get the
1677
+ reverse operation.
1677
1678
1678
1679
1679
1680
Modelling currencies
Original file line number Diff line number Diff line change @@ -198,3 +198,34 @@ macro dump*(x: typed): untyped =
198
198
let r = quote do :
199
199
debugEcho `s`, " = " , `x`
200
200
return r
201
+
202
+ macro distinctBase* (T: typedesc , recursive: static [bool ] = false ): untyped =
203
+ # # reverses ``type T = distinct A``
204
+ runnableExamples:
205
+ import typetraits
206
+ type T = distinct int
207
+ doAssert distinctBase(T) is int
208
+ doAssert: not compiles(distinctBase(int ))
209
+ type T2 = distinct T
210
+ doAssert distinctBase(T2, recursive = false ) is T
211
+ doAssert distinctBase(int , recursive = true ) is int
212
+ doAssert distinctBase(T2, recursive = true ) is int
213
+
214
+ let typeNode = getTypeImpl(T)
215
+ expectKind(typeNode, nnkBracketExpr)
216
+ if $ typeNode[0 ] != " typeDesc" :
217
+ error " expected typeDesc, got " & $ typeNode[0 ]
218
+ var typeSym = typeNode[1 ]
219
+ if not recursive:
220
+ let impl = getTypeImpl(typeSym)
221
+ if $ impl.typeKind != " ntyDistinct" :
222
+ error " type is not distinct"
223
+ getTypeInst(impl[0 ])
224
+ else :
225
+ while true :
226
+ let impl = getTypeImpl(typeSym)
227
+ if $ impl.typeKind != " ntyDistinct" :
228
+ typeSym = impl
229
+ break
230
+ typeSym= getTypeInst(impl[0 ])
231
+ typeSym
You can’t perform that action at this time.
0 commit comments