nimoji icon indicating copy to clipboard operation
nimoji copied to clipboard

compile time

Open pietroppeter opened this issue 5 years ago • 3 comments

import nimoji
echo static ":wave:".emojize

output:

Error: cannot evaluate at compile time: emojiCodemap

pietroppeter avatar Sep 30 '20 10:09 pietroppeter

it is a problem of string tables (but not of tables):

import tables, strtabs

const
  next = {0: 1, 1:2}.toTable # ok
  print = {0: "0", 1: "1"}.toTable # ok
  parse = {"0": 0, "1": 1}.toTable # ok
  read = {"0": "zero", "1": "one"}.toTable # ok
  dict = {"zero": "0", "one": "1"}.newStringTable # fails

Error: invalid type for const: StringTableRef

pietroppeter avatar Oct 01 '20 14:10 pietroppeter

it is in general due to the fact that ref types cannot be used at compile time (manual) and StringTable is implemented as ref type and not as object (while all other tables are object types with optional ref type).

see also https://github.com/nim-lang/Nim/issues/8521

pietroppeter avatar Oct 01 '20 15:10 pietroppeter

a way to support this would be to replace StringTableRef with a Table[string, string] and make sure that checking for a key is done in a (full) case insensitive way.

POC: https://play.nim-lang.org/#ix=2znW

import tables, strutils

const
  map = {"heart": "❤️", "spaghetti": "🍝" }.toTable
  
proc get(dict: Table[string, string], key: string): string =
  dict[key.toLower.replace("_", "")]

const
  heart = map.get("He_Art")

static:
  echo heart

pietroppeter avatar Oct 01 '20 15:10 pietroppeter