Skip to content

slicing in collect macro as add operation doesn't work #18405

@hamidb80

Description

@hamidb80

the compiler gives type mismatch error for slicing in collect macro

Example

import tables, sugar
const document = ["this", "is a", "sequence", "of strings"]

let table1 = collect initTable:
  for word in document:
    # {word: 2}      # works [1]
    # {word[1]: 2}   # works [2]
    # {$word[0..2]: 2} # works [3]
    # {word[0..2]: 2}  # doesn't work [4]
    
    # {2: word}  # works [5]
    {2: word[0..2]}  # doesn't work [6]

echo table1

Current Output

compiler error for [4]

<myCode>.nim(4, 22) template/generic instantiation of `collect` from here
nim-#devel\lib\pure\sugar.nim(312, 29) Error: type mismatch: got <Table[system.char, system.int], string, int literal(2)>
but expected one of:
proc `[]=`(s: var string; i: BackwardsIndex; x: char)
  first type mismatch at position: 0
<...>
expression: `[]=`(collectResult_385875975, word[0 .. 2], 2)

compiler error for [6]:

<MYcode>.nim(4, 22) template/generic instantiation of `collect` from here
C:\Users\HamidB80\.choosenim\toolchains\nim-#devel\lib\pure\sugar.nim(312, 29) Error: type mismatch: got <Table[system.int, system.char], int literal(2), string>
but expected one of:
<...>
expression: `[]=`(collectResult_385875975, 2, word[0 .. 2])

Expected Output

compiles successfully

Additional Information

maybe related to #12383

my environment:

Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-06-20
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions