mratsim / Arraymancer

A fast, ergonomic and portable tensor library in Nim with a deep learning focus for CPU, GPU and embedded devices via OpenMP, Cuda and OpenCL backends
https://mratsim.github.io/Arraymancer/
Apache License 2.0
1.34k stars 96 forks source link

Span slicing inside dynamic type procs fails to compile #43

Closed edubart closed 7 years ago

edubart commented 7 years ago

Minor test case:

import arraymancer

proc boo[T](): T =
  var a = zeros([2,2], int)
  echo a[1,_]

discard boo[int]()

When compiling the following error is shown:

test.nim(4, 12) Error: undeclared identifier: '_'

Removing dynamic type [T] from the proc the code works fine.

mratsim commented 7 years ago

I've reproduced it and reported it to Nim upstream.

It seems like for generic procs, there is an identifier resolution happening before the macros kick-in and replace "_".

I may have a workaround by exporting a dummy "_" but I don't want to pollute namespace with that symbol. And it's used in tuple pattern-matching as well and maybe by other libs.

mratsim commented 7 years ago

You can workaround that issue by using mixin _ from module macros in the procs that uses "_"

edubart commented 7 years ago

This workaround does not work with more complex silicing, like x[_, _, ^1..0|-1]

mratsim commented 7 years ago

Not sure which identifier poses the issue here, with seq I can "solve" it by adding everything as mixin :/

proc foo4[T](): CustomSeq[T] =
  result.data.newSeq(10)
  # weird fancy corner case that feels special and wants special treatment
  mixin _,`|-`
  # why?
  echo result[_, _, ^1..0|-1]

Case is closed upstream, but I really think generics have an issue as detailed by Krux