Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Could you elaborate on why do you need your custom generic function to return pointer?

Also, how 'ToPointer(v)' is better than writing just plain '&v'?



If you have e.g. a *string you can't populate it with &"hello world." You have to first assign the string to a variable and then take the address of that variable. This comes up very often in tests and is super inconvenient. Many people write TypeNamePtr functions so that you can call e.g. StringPtr("hello world") to express it in one line and without creating any names. Without generics you had to do this individually for every type.


Sometimes you need to instantiate a struct, usually in a test, that contains a bunch of pointers to built in types. Say a struct representing a json request body with optional fields. You can’t just take a pointer to a string literal `&”foo”` and making a variable for everyone of these fields is cumbersome so you have a function that returns a pointer to the value it’s given.

I’ve personally never needed it outside of tests.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: