diff --git a/changelog.md b/changelog.md index abb761b4f..2c7cdeed0 100644 --- a/changelog.md +++ b/changelog.md @@ -114,6 +114,7 @@ - Add `os.normalizeExe`, eg: `koch` => `./koch`. - `macros.newLit` now preserves named vs unnamed tuples; use `-d:nimHasWorkaround14720` to keep old behavior - Add `random.gauss`, that uses the ratio of uniforms method of sampling from a Gaussian distribution. +- Add `typetraits.elementType` to get element type of an iterable. ## Language changes - In the newruntime it is now allowed to assign to the discriminator field diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 317376405..346ac06d4 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -93,6 +93,18 @@ since (1, 1): type StaticParam*[value: static type] = object ## used to wrap a static value in `genericParams` +since (1, 3, 5): + template elementType*(a: untyped): typedesc = + ## return element type of `a`, which can be any iterable (over which you + ## can iterate) + runnableExamples: + iterator myiter(n: int): auto = + for i in 0..