I like the strict funcs experimental feature, and I like using it in my side projects. But I'm having a small issue and wanted to see what others think about it.
Here's the setup: I have a reference type with a private member that is a table. I want a pure function to be able to read from that table (and do other things too). The problem is that because I'm accessing the private member through a reference, it is by default mutable, and so calling a proc on it in turn will choose the mutable proc over the immutable one if there is a choice. This comes up with the [] proc for tables.
Here's a toy example:
import tables
type
Foo = ref object
data: Table[string, int]
func readData(f: Foo, k: string): int =
f.data[k]
if isMainModule:
let f = Foo(data: initTable[string, int]())
f.data["a"] = 1
echo f.readData("a")
This will compile, but not if you use --experimental:strictFuncs. The issue is in readData: it selects the [] proc that returns var B, because the compiler believes f.data to be mutable. In my opinion, the compiler should treat f and f.data as immutable in this context, rather than treating it as mutable and then creating an error. If others feel the same way too, I'm happy to create an issue in Github detailing the desired behavior for the compiler devs to consider.
That being said, is there a standard workaround for this? Can I force the compiler to see f and f.data as mutable?
Use var and var Foo.
{.experimental: "strictFuncs".}
import tables
type
Foo = ref object
data: Table[string, int]
func readData(f: var Foo, k: string): int =
f.data[k]
if isMainModule:
var f = Foo(data: initTable[string, int]())
f.data["a"] = 1
echo f.readData("a")