You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use toOpenArray for a library I'm working on, in order to get around the limitation that procedures cannot return an openarray I am using templates. The problem is that as soon as I introduce any more statements to my template I start getting errors.
Example
templatenewOpenArray(x: varstring, size: int): openArray[char] =var z =1toOpenArray(x, z, size)
templatedoSomethingAndCreate(x: varstring): openArray[char] =let size =12newOpenArray(x, size)
var x ="asdqweqweqweqwrqwrqwrqwr"# echo toOpenArray(x, 0, 2)# echo newOpenArray(x, 12)echodoSomethingAndCreate(x)
Current Output
a3.nim(13, 26) Error: invalid context for 'toOpenArray'; 'toOpenArray' is only valid within a call expression
ccgcalls.openArrayLoc only checks for the mSlice magic if it appears at the top-level, it doesn't check if it's nested inside a nkStmtListExpr. The code instead moves into the second branch and calls initLocExpr which eventually enters code that raises the exception.
I was able to come up with some really hacky workaround in the compiler code. I'm curious what the correct way to fix this is though, where should this nkStmtListExpr be processed?
Additional Information
$ nim -v
Nim Compiler Version 0.20.99 [MacOSX: amd64]
Compiled at 2019-08-06
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: 8407a574992ebd6bccec647a902cf54a4de8db18
active boot switches: -d:release
The text was updated successfully, but these errors were encountered:
I'm trying to use toOpenArray for a library I'm working on, in order to get around the limitation that procedures cannot return an openarray I am using templates. The problem is that as soon as I introduce any more statements to my template I start getting errors.
Example
Current Output
Expected Output
Possible Solution
ccgcalls.openArrayLoc only checks for the mSlice magic if it appears at the top-level, it doesn't check if it's nested inside a nkStmtListExpr. The code instead moves into the second branch and calls
initLocExpr
which eventually enters code that raises the exception.I was able to come up with some really hacky workaround in the compiler code. I'm curious what the correct way to fix this is though, where should this nkStmtListExpr be processed?
Additional Information
The text was updated successfully, but these errors were encountered: