Nim/lib/pure/typetraits.nim
Timothee Cour 6ce3949c8a
add isNamedTuple; make $(1, 2) be (1, 2) instead of (Field0: 1, Field1: 2) which leaked implementation detail (#10070)
* add `isNamedTuple`; make $(1, 2) be (1, 2) instead of leaking implementation detail (Field0: 1, Field1: 2)
  fixes this: #8670 (comment) /cc @alehander42 @Vindaar @mratsim

* Note: isNamedTuple is useful in other places, eg #10010 (comment)

* move isNamedTuple to helpers.nim to avoid exposing new symbol to system.nim

* remove workaround in tests/vm/tissues.nim failing test now that #10218 was makes it work
2019-01-08 18:37:25 -08:00

68 lines
1.9 KiB
Nim

#
#
# Nim's Runtime Library
# (c) Copyright 2012 Nim Contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module defines compile-time reflection procs for
## working with types
include "system/helpers" # for `isNamedTuple`
export system.`$`
export isNamedTuple
proc name*(t: typedesc): string {.magic: "TypeTrait".}
## Alias for system.`$`(t) since Nim v0.20.0.
proc arity*(t: typedesc): int {.magic: "TypeTrait".} =
## Returns the arity of the given type. This is the number of "type" components or
## the number of generic parameters a given type ``t`` has.
runnableExamples:
assert arity(seq[string]) == 1
assert arity(array[3, int]) == 2
assert arity((int, int, float, string)) == 4
proc genericHead*(t: typedesc): typedesc {.magic: "TypeTrait".}
## Accepts an instantiated generic type and returns its
## uninstantiated form.
##
## For example:
## seq[int].genericHead will be just seq
## seq[int].genericHead[float] will be seq[float]
##
## A compile-time error will be produced if the supplied type
## is not generic.
proc stripGenericParams*(t: typedesc): typedesc {.magic: "TypeTrait".}
## This trait is similar to `genericHead`, but instead of producing
## error for non-generic types, it will just return them unmodified.
proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".}
## This trait returns true iff the type ``t`` is safe to use for
## `copyMem`:idx:. Other languages name a type like these `blob`:idx:.
when isMainModule:
static:
doAssert $type(42) == "int"
doAssert int.name == "int"
const a1 = name(int)
const a2 = $(int)
const a3 = $int
doAssert a1 == "int"
doAssert a2 == "int"
doAssert a3 == "int"
proc fun[T: typedesc](t: T) =
const a1 = name(t)
const a2 = $(t)
const a3 = $t
doAssert a1 == "int"
doAssert a2 == "int"
doAssert a3 == "int"
fun(int)