|
|
|
|
|
by go-nil-why
464 days ago
|
|
There is no practical difference between function useFoo(f: Foo) { f.Use() }
var foo: Foo
useFoo(foo)
and func useFoo(f *Foo) { f.Use() }
var foo *Foo
useFoo(foo)
in this context. References vs pointers are equivalent here. |
|
A better example will be:
That go code would be functionally equivalent to your Typescript code and if everyone used pointers like references in Go, like the above code, then you wouldn’t have any nil pointer bugs.Nil pointer bugs happen when you need to use pointers as pointers. (Shock horror!) And sometimes you do need a pointer with no object attached when solving specific problems are aren’t well suited for Typescript.
Python and Typescript doesn’t have pointers. Those languages made different trade offs so they lose performance and memory management controls as a result.
It’s no secret that Python needs to call FFIs to (for example) C++ code when performance and memory management concerns arise. Likewise with node too. In fact I’m in the process of debugging a 3rd party Rust library for Node that’s not handling pointers correctly and thus causing sporadic crashes of the node runtime. And that Rust library only exists in the first place because JavaScript doesn’t have primitives to write the required code natively in node at the performance required for scale.
Maybe WASM will be the saviour here. But it feels like we are now just piling on more abstractions between the code and CPU rather than trying to learn how to use our existing set of tools better.