Nim: initTable with self-declared Table types

Created on 5 Feb 2015  路  11Comments  路  Source: nim-lang/Nim

If you declare a type which is a Table with a given combination of key and value types, as far as I can see, it's not possible to call initTable with that type, since it expects two parameters. Example:

import tables

type
  CustomTable = Table[int, string]

var
  table1 = initTable[int, string]()  # works
  table2 = initTable[CustomTable]()  # doesn't

The reason I think that would be useful is if you have a routine that returns a custom table, the instantiation of result looks awkward:

proc tableFromFile(filename: string): CustomTable =
  result = initTable[int, string]()
  # do something more...

From that code it's not clear that you are actually creating a table of the correct type until you look up its definition. And if you ever change its definition, you'd have to remember to adapt the call to initTable accordingly.

Feature Generics Stdlib

Most helpful comment

I've proposed how the entirely standard library should switch to the pattern above here:
https://github.com/nim-lang/Nim/issues/7832

All 11 comments

I think it should work like this instead:

var
    table3: CustomTable = initTable()  # this should work

proc tableFromFile(filename: string): CustomTable =
  result = initTable()

This works for creating new sequences with @[] already (but not with newSeq):

type
  CustomSeq = seq[string]

var
  seq1 = newSeq[string]()
  seq2: CustomSeq = @[]

proc seqFromFile(filename: string): CustomSeq =
  result = @[]

Agreed, that would be even better.

Another call for return type overloading?

Another call for return type overloading?

I don't know, how does @[] do it?

This is more in the realm of type inference with regards to return types

https://github.com/nim-lang/Nim/issues/1070 was closed a few days ago as "not going to happen", and this is essentially a special case for it - should we close it as well?

I don't see it arguing for overloading by return type. Instead it argues for more type inference for generics. So no, this should remain open. :-)

On a second read, I agree, there's no overloading here, just type inference from the return type.

Why not define an

proc init[K, V](t: var Table[K, V])

# And then
proc tableFromFile(filename: string): CustomTable =
  result.init()

This pattern would be really useful for object fields of Table type.

type Foo = ref object
  t: Table[string, int]

proc newFoo(): Foo =
  result.new()
  result.t.init()
  # instead of:
  result.t = initTable[string, int]()

Downsides of the current initTable is that you have to spell out the K, V again and again (not DRY), and that the result might get unnecessarily copied in memory.

I've proposed how the entirely standard library should switch to the pattern above here:
https://github.com/nim-lang/Nim/issues/7832

This (and much more) is now tracked and discussed in the mentioned @zah's RFC here: https://github.com/nim-lang/RFCs/issues/48

Was this page helpful?
0 / 5 - 0 ratings