3 ms·
It took me a moment to parse, but that snippet is actually pretty straight-forward when you know that Nim compiles down to C (or C++). type Vector* {.importc
by majewsky 10y ago
It took me a moment to parse, but that snippet is actually pretty straight-forward when you know that Nim compiles down to C (or C++).
type Vector* {.importcpp: "std::vector", header: "<vector>".}[T] = object
This obviously creates a type "Vector" whose underlying implementation is C++'s "std::vector". (I don't know how writeable it is, but it is* readable.)
template `[]=`*[T](v: var Vector[T], key: int, val: T)
Function declaration syntax for operator overloads is hard to make readable (as in: familiar). In fact, this is about as readable as it can get (although I'm confused by the "*[T]").
= {.emit: [v, "[", key, "] = ", val, ";"].}
As I said, when you know that the compiler generates C code, this makes a lot of sense. It's a compiler instruction to compose a string of C code.
- evacchi 10y ago> (although I'm confused by the "[T]"). the `*` marker means that the proc/template is "public" (not local to the module)
- vgbm 10y agoTo supplement the other response, [T] declares a generic type, so [T] is a public method with the generic type T. I'd agree though that the is initially hard to parse, especially when taking C and C++ into account, where * means something entirely different.