I just tried to optimize the gintro generated code a bit.
One known issue is the alias for proc names -- some people like the get and set prefixes for getters and setters, others don't. Like in container.getChild() vs container.child().
Some years ago I tried to use plain const proc names as alias, but there was an issue, so I generated the procs twice. No problem, but not optimal for compile time, and only optimal for executable size when the compilers are smart.
So I tried again using const, and now I remember the issues:
#proc getLength(i: int): int=
# sizeof(i)
proc getLength(s: string): int =
s.len
const
length = getLength
echo length("alias")
Works fine when the topmost proc is not present -- but for gtk with more then 10k procs there is often a conflict.
Maybe now, some years later, there is a solution? Can we give full parameter list for proc consts? Or use templates? Or something like "when compiles" to skip conflicts? Or continue generate procs twice?
I have just added that to my code generator, but the funny fact is that this will not work when there is only one proc with that name:
#proc getLength(i: int): int =
# sizeof(i)
proc getLength(s: string): int =
s.len
const
length = (proc(s: string): int)(getLength)
echo length("alias")
/tmp/hhh/t.nim(8, 3) Error: invalid type for const: proc (s: string): int{.closure.}
Maybe I can collect all proc names in a hashset and generate different code when there are single or multiple names in the module.
Wouldn't an additional proc suffice?
proc length[T](t: T): int {.inline.} = getLength(t)
properly fixed here: https://github.com/nim-lang/Nim/pull/11822 nim has a real alias that works with any symbol #11822
proc getLength(i: int): int = sizeof(i)
proc getLength(s: string): int = s.len
# const length = getLength # Error: cannot generate VM code for getLength
length := getLength # works
echo length("alias")
Great, thanks!
I do not need aliases often, but for a few rare cases I really missed it.
For procs const was not really working perfect in the past, and I was never fully happy using templates to just alias a var.
As I understand it, this is still a pull request that hasn't been merged.
I think having aliases would be great, but I also see the potential conflict with already defined operators that is mentioned in the comments of the PR.
Will you alias work for the result variable too? Would be great -- I had often procs like
https://github.com/StefanSalewski/cdt/blob/master/src/cdt/dt.nim#L92
where the symbol result is used really often. And the disadvantage of the term result is, that it is long and name does not indicate the true meaning of that value. No problem for short procs, but for longer procs, or when the term result is used very often, I would really like an alias.
I know that this is only a PR, but as timothee is one of the really smart devs, it should be merged soon...
Will you alias work for the result variable too? Would be great -- I had often procs like
it works, I just added a test case for that see https://github.com/nim-lang/Nim/pull/11822/files#diff-ef6ec6154188e96632ac5bf114a7c1faR131 (simply use r:=result and it just works)