Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UFCS/MCS broken for routines accepting iterables (via untyped params) eg toSeq #13595

Open
nothratal opened this issue Mar 6, 2020 · 4 comments

Comments

@nothratal
Copy link

The toSeq-template of sequtils has no support for UFCS when using it together with the keys-iterator of tables.

Example

import tables
import sequtils

var tab = initTable[string, seq[int]]()

tab["monday"] = @[1,2,3,4,5,6]
tab["tuesday"] = @[5,7,2,5,2]
tab["wednesday"] = @[1,2,3,4,5,6]
tab["thursday"] = @[1,2,3,4,5,6]
tab["friday"] = @[1,2,3,4,5,6]
tab["saturday"] = @[1,2,3,4,5,6]
tab["sunday"] = @[1,2,3,4,5,6]

echo tab.keys.toSeq

Current Output

/usercode/in.nim(15, 9) Error: undeclared field: 'keys' 
  found 'tables.keys(t: TableRef[keys.A, keys.B]) [declared in /playground/nim/lib/pure/collections/tables.nim(1127, 10)]' of kind 'iterator'
  found 'tables.keys(t: Table[keys.A, keys.B]) [declared in /playground/nim/lib/pure/collections/tables.nim(694, 10)]' of kind 'iterator'
  found 'tables.keys(t: OrderedTable[keys.A, keys.B]) [declared in /playground/nim/lib/pure/collections/tables.nim(1674, 10)]' of kind 'iterator'
  found 'tables.keys(t: CountTableRef[keys.A]) [declared in /playground/nim/lib/pure/collections/tables.nim(2724, 10)]' of kind 'iterator'
  found 'tables.keys(t: OrderedTableRef[keys.A, keys.B]) [declared in /playground/nim/lib/pure/collections/tables.nim(2068, 10)]' of kind 'iterator'
  found 'tables.keys(t: CountTable[keys.A]) [declared in /playground/nim/lib/pure/collections/tables.nim(2459, 10)]' of kind 'iterator'

Expected Output

Expecting it to compile

Additional Information

  • Using echo toSeq(tab.keys) works correctly:
    @["wednesday", "thursday", "saturday", "friday", "tuesday", "monday", "sunday"]
  • Tested it on Linux & Windows & play.nim-lang.org:
> nim --version
Nim Compiler Version 1.0.6 [Windows: amd64]
$ nim --version
Nim Compiler Version 1.0.6 [Linux: amd64]
@andreaferretti
Copy link
Collaborator

Related

nim-lang/RFCs#512
#9219
https://nim-lang.org/docs/manual.html#templates-limitations-of-the-method-call-syntax

@timotheecour
Copy link
Member

timotheecour commented Mar 7, 2020

it's not specific to toSeq, it's a sigmatch compiler limitation:

template fun(a: untyped) = discard
iterator iter(): int = yield 10
fun(iter()) # ok
# iter().fun() # CT error

IMO it's fixable (I had a POC for it IIRC).
one way would be allowing passing an iterator as iterator {.inline.} instead of untyped, eg:

template fun(a: iterator {.inline.}) = discard
iterator iter(): int = yield 10
iter().fun()

@timotheecour timotheecour changed the title no UFCS support on toSeq with tables keys iterator UFCS/MCS broken for routines with unytyped (eg iterables) params, eg toSeq Dec 17, 2020
@timotheecour timotheecour changed the title UFCS/MCS broken for routines with unytyped (eg iterables) params, eg toSeq UFCS/MCS broken for routines accepting iterables (via untyped params) eg toSeq Dec 17, 2020
@timotheecour timotheecour mentioned this issue Feb 27, 2021
8 tasks
@denise-amiga
Copy link

I understand that this problem is related to the same parser limitation

import tables

let txt = "zbabaacbaabz"
var ct_fine = toCountTable[char](txt)
var ct_fail = txt.toCountTable[char]()   # <--- no compile

@beef331
Copy link
Collaborator

beef331 commented May 29, 2023

Except there is a solution for that txt.toCountTable[:char]()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants