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

It is very occasionally useful when you need to define a self-referential data structure, i.e. something like:

  struct foo { foo& p; ... };
  
  foo x{x, ...};
Still, this is hardly a good justification for it to be the default behavior. There's a reason why ML has `let rec`.



Thank you for this, it illustrates an important distinction. (Your example would be clearer if you used a pointer instead of a reference)

Address of variable does not depend on it's value, and can be known and used before the variable is defined. At no point in your example value of "x" is used before the end of initialization.

However, in order to allow that, the language goes and allows the use of uninitialized value too. That is just plain horrible design.


> Your example would be clearer if you used a pointer instead of a reference

I actually used the reference deliberately here because, unlike a pointer, a reference cannot be rebound.

But yes, I agree, this isn't good design. Off the top of my head I can think of at least one way to enable the same thing with a tiny bit of special (and rather obvious) syntax without introducing a footgun. It wouldn't even need any new keywords!

  foo x{this x, ...};




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: