Hi there,
I'm working through the very excellent "Write an Interpreter in Go" (https://interpreterbook.com/)
I'm trying to follow the book but implementing in nim rather than Go, but running into a wall trying to translate Go into Nim.
For example passing pointers to objects or returning pointers to objects.
An example in Go
/* A lexer */
type Lexer struct {
   input string
   position int
   readPosition int
   ch byte
}
/* Function to create a new lexer */
func New(input string) *Lexer {
  l := &Lexer{input: input}
}
 Translating to nim I have:
type TLexer = object
  input     : string
  position  : int
  readPosition : int
  ch        : byte
proc new(input : string) : TLexer =
  let l = new TLexer(input, 0, 0, 0)
  return l
but I know I'm not doing this well. #1 Not sure how to create a Lexer object #2 Not sure how to return a pointer from a proc.
Initially I tried defining the Lexer as
type Lexer = ref object
 ...
 But that did not compile.
I also tried defining the proc as
proc New(input: string): ref Lexer =
 ....
But also didn't compile.
The nim way of working with objects is a bit new to me. I'm sure it's something very simple but not sure what is that little bit I'm nt getting.
if you define Lexer as a reference type there is then no need to use the ref keyword anymore, your New function (in Nim the convention is to call it newLexer) would be simply
proc newLexer(input: string): Lexer =
    ....
 and, under the hood, it would return a garbage collected pointer to an heap-allocated objectThanks so much for this...pointer ;)
Works like a charm now.