Hey, played some more with the neovim host thingy. It works well enough but some notational things annoy me.
I tried to abstract the parsing as much as possible so the param parsing for rpc is just generate out of the proc definition. Works pretty well so far but some more general notational stuff annoyed me:
When parsing you could just express everything as object variants which works but is annoying to handle if you know the types before hand. You could create separate methods for everything but that would be annoying to use as well. You could use static dispatch to make it cleaner:
var i: int
s.unpack i
but that requires two very repetitive lines for every item. Return type based overloading doesn't work yet but a generic return type sounds like the best next thing!
proc unpack[T](s: Stream): T =
s.unpack result
var i = s.unpack[int64]()
> Error: cannot instantiate: 'T'
So, is there a way to do this?
The following works for me:
import strutils
proc unpack[T](v: string): T =
when T is int:
result = parseInt(v)
when T is string:
result = $v
var s = "123"
assert(unpack[string](s) is string)
assert(unpack[int](s) is int)
echo unpack[int](s)
echo unpack[string](s)
As does this:
import strutils
proc unpack(t: typedesc[string], v: string): string = $v
proc unpack(t: typedesc[int], v: string): int = parseInt(v)
proc unpack[T](v: string): T =
unpack T, v
var s = "123"
assert(unpack[string](s) is string)
assert(unpack[int](s) is int)
echo unpack[int](s)
echo unpack[string](s)
# which could be done like this too
echo unpack(int,s)
echo unpack(string,s)
# or like this for added fun
template `as`*(x: untyped, t: typedesc): untyped = unpack(t,x)
echo s as int
echo s as string
Alright, thanks! I think that still might force me the change the entire msgpack implementation but at least it compiles perfectly fine!
Also I shouldn't be surprised by this anymore but it is nice how nim just transforms everyting into arguments when needed.
Edit: Actually,
proc unpack[T](s: Stream): T {.inline.} =
s.unpack result
echo unpack[int64](s)
works perfectly fine. It is s.unpackint64 that breaks stuff. Which seems weird but at least it mostly works without rewriting everything.