Nim: Crash when trying to use `type.name[0]`

Created on 3 Dec 2019  路  2Comments  路  Source: nim-lang/Nim

I tried to use [0] for a type name and it leads to crash

Example

import typetraits, strutils

type
  AbComponent* = object

template local*(expression: untyped): untyped {.dirty.} =
  # this fails
  # $(expression.type.name[0]) 
  expression.type.name[0 .. 0]


var self = AbComponent()
echo local(2)

Current Output

If you comment out, it crashes with

fatal.nim(39) sysFatal
Error: unhandled exception: index out of bounds, the container is empty [IndexError] 

Expected Output

Running correctly and returning the first char

Possible Solution

  • Don't know yet

Additional Information

  • It works if i use [0 .. 0] so this is a workaround
$ nim -v
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2019-12-02
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 2acf74d458accf65d969199a389c8dbc2eedab55
active boot switches: -d:release

Most helpful comment

Not related to procs afaict.
A bit more minimal example:

import typetraits
echo int.name[0]

All 2 comments

Not related to templates afaict.
A bit more minimal example:

import typetraits
proc local(expression: int): char = expression.type.name[0]
echo local(1)

Not related to procs afaict.
A bit more minimal example:

import typetraits
echo int.name[0]
Was this page helpful?
0 / 5 - 0 ratings