Better HN
topnewbestaskshowjobs
Log inSign up

ninkendo

8,671 karma · joined Dec 19, 2010 (15y ago) · 2,475 submissions

Submissions

comment 18h ago · link · parent

I completely agree, but the point was to show Rust code that does the same thing as the (equally unsafe) C++ code, and particularly that it doesn't treat built-ins any different than other types. But nobody should be writing code like this in either language.

To me the whole point of an "uninitialized variable" is to declare it in one place and then assign it in exactly one code path, which is exactly what rust supports without any unsafe code:

  let i: i32;
  if is_tuesday {
      i = 0;
  else {
      i = 1;
  }
  foo(i);
is already perfectly valid, safe rust code, because the compiler knows that all code paths result in assignment before use. And the answer to "what happens on that first line?" is "nothing", because it's just a declaration. In the equivalent C++ you'd be running an arbitrary constructor on the first line, and hoping the compiler optimizes it away. Or if the default constructor doesn't exist or is deleted, you simply can't code that way.

comment 18h ago · link · parent

Objective-C has implementation inheritance but doesn't have constructors, I don't think constructors are essential there. As I said in the OP, `Cow::new()` can easily just call `Animal::new()` before continuing to initialize its own fields, etc.

Heck, Smalltalk is the original OOP language and has implementation-based inheritance, and construction is done through ordinary methods on class objects.

comment 19h ago · link · parent

> However, create-and-return makes in place construction difficult

This is definitely the closest thing to an answer in this thread. This is something that languages without constructors do struggle with, yes, and as other commenters pointed out, Rust sees this as a performance issue and is working on it.

Thanks for the informative reply.

comment 19h ago · link · parent

I literally had `MaybeUninit::uninit().assume_init()` first in the comment but rewrote it with `std::mem::uninitialized()` because I thought it would be more readable to a C++ crowd, but perhaps your right, I should have left it as-is.

comment 19h ago · link · parent

They could have just as easily said "C semantics are used when we know we're parsing C code" and declared that C++ code doesn't have implicit constructors. No, the decision to have constructors was made because it's what they wanted to do when the language was designed. You can't lean on "but C" as an excuse when those problems are so easily solved other ways.

comment 19h ago · link · parent

> It is up to the constructor to only initialize storage, or do whatever it feels like to have values assigned to it.

Right, see the "I guess the argument might be" part above... I find this to be a very questionable design choice, leaving you to have to go and read a header somewhere to determine whether `Foo f;` is safe or not. But it's not really a choice, is it? C++ had to do it this way to be compatible with how C programs were written back in the 1970s.

> By the way, already in the 16 bit days you could configure to have warnings as errors for variables being used without initialisation.

Ah yes, the "but C can be configured to be not-insane" argument, one of my favorite pjmlp-isms, right up there with "but $LANG's compiler is written in C++" and "but you already get memory safety with a GC". Keep on doing you!

comment 20h ago · link · parent

Yep, that's std::mem::zeroed(), which just writes zeroes into the allocated memory regardless of its type... also only available in unsafe code.

    #[derive(Debug)]
    struct Foo {
        _i: i32,
    }

    fn main() {
        let x: i32 = unsafe { std::mem::zeroed() };
        println!("x={x}");
        let f: Foo = unsafe { std::mem::zeroed() };
        println!("f={f:?}");
    }
prints:

    x=0
    f=Foo { _i: 0 }
It's unsafe because if Foo contained a String, it would be string with a null pointer inside it and you'd crash trying to read it. If you want "Default" values instead of zeroed (ie. you don't want to run unsafe code), that's what the Default trait is for, and this becomes:

    #[derive(Debug, Default)]
    struct Foo {
        _i: i32,
    }

    fn main() {
        let x: i32 = Default::default();
        println!("x={x}");
        let f: Foo = Default::default();
        println!("f={f:?}");
    }
and prints the same thing.

comment 20h ago · link · parent

As I replied in a sibling comment to yours, at least one language without constructors (Rust) has the exact same semantics for built-in types vs library types here. You must initialize in typical code, and the escape hatch is std::mem::uninitialized() which lets you work with uninitialized data, and this is true for any type, built-in or otherwise.

comment 21h ago · link · parent

In C++, `int x;` leaves the int uninitialized, which isn't really constructing at all. In rust (which I program in lately so I'll use it as the example) you simply can't use uninitialized variables in the first place without unsafe code, so the equivalent would either be:

  let x: i32;
  foo(x); // compiler error
or if you really wanted to use uninitialized memory,

  let x: i32 = unsafe { std::mem::uninitialized() }; // compiler warning
  foo(x);
which, in rust, can be done with any other type too (substitute i32 for any T and the above examples are identical.) I don't see anything different about built-in types here vs standard ones, other than the compiler has literals for the built-ins, which is more a consequence of rust not allowing literals for library types. But that's a very different discussion.

I guess the argument might be that for C++, they want the author of the type to determine whether the value should be left uninitialized on declaration/construction, instead of the caller. Which is... not exactly the semantics I would have chosen.

comment 21h ago · link · parent

I get that for C++ to have all this overloading it needs constructors, but Swift doesn't take the same bet as C++ does when it comes to overloading, yet it still has first-class constructors anyway. So do lots of other languages (Java, C#, TypeScript, etc.) Swift is especially interesting because it's basically a successor language to Objective-C, and Objective-C did not have first class constructors (`-init`/`-initWithFoo:` is just a convention), they chose to add it anyway.

comment 21h ago · link · parent

I don't see the link between constructors and whether or not built-in types have special treatment. Care to enlighten the class?

comment 21h ago · link · parent

Constructors are such an odd duck to me. On the one hand, you have languages like C++ and Swift which have construction as a first-class concept, with tons of rules you need to memorize (Swift's "convenience" vs "designated" initializers are a real treat to unpack).

Then you have languages like Rust, Go, Zig, etc which don't have them at all, constructors are just static functions in the namespace of the type, which return an instance of that type.

For the life of me I've never understood why you'd want the former. I'm sure there's a reason, but I never miss it when using languages in the latter category.

The explanation you typically hear is about OO and inheritance, but I don't buy it: If Animal had a ::new() that initialized private state, Cow could have a ::new() too that could call Animal::new() if it wanted, then do further initialization, etc... I don't think there's anything about inheritance that says constructors are a must. (Edit: In fact, ObjC does exactly this. I should have remembered that given that I did ObjC for a living for a while.)

comment 22h ago · link · parent

[delayed]

comment 22h ago · link · parent

> proving that factoring is in P would not have any practical consequences.

I would change that to "might not have any practical consequences". It could very well be that the proof that factoring is in P is made by discovering a polynomial-time algorithm for factoring. In fact that would be my default assumption if you told me "someone proved factoring is in P": they must have found an algorithm that does it. (I'm aware there's other ways to prove it, but to my dumb brain, proving the existence of X is easiest done by finding X.)

There's been lots of times in history where we thought a problem was in NP, then someone found a P solution for it. In fact that's one of the very reasons we don't know if P=NP or not.

comment 1d ago · link · parent

Again, you're only showing the part I already said I agree with: That Jobs and NeXT don't think users should have to be exposed to the UNIX parts.

Your claim was that NeXT would have written a completely independent operating system core with no relationship to UNIX whatsoever, and the only reason they didn't was something hand-wavey about universities. Care to back that claim up and not respond with red herrings about user interface?

comment 2d ago · link · parent

None of those links or quotes support your claim.

The contrapositive would be that NeXT would have written a separate OS foundation that had nothing to do with UNIX, and your claim is that the only reason they didn't do this was to get universities to buy it. But your links only emphasize how much they wanted to hide the underpinnings (which is a good idea in general, since they were going for, y'know, a GUI.)

A much simpler explanation is that they needed UNIX's design as a well-understood and proven technical foundation, but were determined to hide that fact as much as possible, because they didn't believe in the CLI as the future of the user interface. But for markets where users actually wanted UNIX, they were happy to advertise that it was there.

comment 3d ago · link · parent

I won’t believe for a second that you can lint for “does this make unrelated changes” or “is this consistent with the rest of the codebase” with a regex match. I’m not sure you even read GP’s post.

comment 5d ago · link · parent

Question to those who know more about this than me:

Will we be able to get an actual simulation of a system, which follows the Navier-Stokes equations, that will actually show the blow-up happen in real time, so we can potentially gain an intuitive understanding of where the equations don’t match reality?

The OpenAI release a couple months ago had a rendering of some arrows ascending in a tornado shape, but I don’t know if that’s just an “artist’s rendition” kind of thing or not… it would be interesting to see if a software simulation that simply followed the NS equations could be given some start conditions and then blow up to infinity, in a way I could see for myself. But I don’t know if the proof actually works that way.

comment 5d ago · link · parent

Probably the same reason I forgot about Santa Claus so quickly when I was 5. When you learn better, the old explanations don't make sense any more.

Was I happier when I believed in Santa Claus? Maybe. I'm sure lots of Christians who believe in heaven are happier about the prospect of death than I am. But you can't unlearn these kinds of things, you can only delude yourself.

comment 6d ago · link · parent

Hyrum's law doesn't mean it's ok to knowingly make sweeping changes to your API... yes we all accept that every change might break someone. But that's why locking versions and immutable builds are a thing, it's expected that you should test everything every time you update, because there's always the possibility you're relying on undocumented/unsupported behavior.

But then you have projects that just decide every other month that the abstractions they just introduced are now out of style and decide to rework everything. Changing names of functions, taking formerly-public types and making them private, utterly (and intentionally) breaking every single person that uses the library. I think we can agree that these types of changes are distinct from what you'd put under hyrum's law.

> The alternative to semver is not "projects don't make breaking changes", it's the old bad "projects make breaking changes randomly from release to release, and without carefully reading the changelog you will never know". But back then dependencies were rare, releases were a big thing, and people were OK with reading changelogs. At our current scale this is impossible.

I don't disagree with this, but you can't separate the good from the bad with semver. It makes bad engineering practices easier to spot, yes. But it also normalized them. Intentionally breaking compatibility in a software library is bad engineering practice. SemVer lets people pretend that's not true because they can increment a version number and skirt all blame for it.

(Edit: I'd be happy if the semver quasi-rfc at least paid lip service to the effect of "Backward incompatible changes SHOULD NOT be introduced a public API" or something, but it doesn't even say anything of the effect, just "Major version X MUST be incremented". The FAQ even says "Deprecating existing functionality is a normal part of software development and is often required to make forward progress", which just reinforces the attitude. It shouldn't be "normal" at all... a better wording might be "Deprecating existing functionality is regrettable but sometimes required to make forward progress.")

Again I want to stress that the actual thing I'm against, is the cavalier attitude towards backward compatibility. Like this thread starting from someone saying "I wanted to go into $crate and change everything and was met with resistance!" as if we should be sympathetic to them. No, I'm sympathetic to the maintainers of $crate who understand that if you don't like its API's you should start a new crate, not simply change an existing one to suit your tastes. We need more of this resistance.

comment 1w ago · link · parent

OP didn’t say anything about Rust, by the way. This post comes off as being defensive about a point nobody made.

comment 1w ago · link · parent

Sometimes you get what the installer has in stock, I didn’t really “pick” Chamberlain, it’s just what the guy put in when I paid him to “fix my garage door”. For a lot of other people it’s just what comes with the house.

The correct answer is to just let your garage door opener be dumb, and buy a RatGDO, which works with any dumb opener including chamberlains: https://ratcloud.llc/

I like to think chamberlain sells their openers at a slightly lower price than they otherwise would, hoping to recover it on subscriptions. So by buying their opener plus a RatGDO, I’m hurting their bottom line just a tiny amount. It makes me feel good at least.

comment 1w ago · link · parent

Yeah I guess my point about renaming vs version-bumping is a bit weak with respect to older versions actually being maintained. But really I just want there to be a lot more friction for breaking compatibility in libraries, and "start a new project" is a good example of the level of effort it should require if you want to intentionally break compatibility.

The problem with SemVer is it encourages you to "move fast and break things", constantly bumping major versions as a standard practice, rather than thinking very deeply about what abstractions you make public in your library (often minimizing the surface area as much as possible) to avoid painting yourself into a corner, and being extremely deliberate about any time you need to break the API in order to make a change. Ideally forking things with the changes and releasing the new library "all at once" once enough accumulated changes have built up, rather than bumping the major once every time.

The real issue I'm complaining about is the cavalier attitude towards breaking changes that library maintainers tend to have, and how much I believe SemVer caused it. If this attitude was not prevalent I'd have no problems with SemVer.

comment 1w ago · link · parent

> API was stable and every slight step towards improvement was met with... resistance

(Putting my flame suit on here, but...) I think that's a good thing, and I wish this were the norm with more libraries in the open source world. Once you hit a stable version, breaking changes should happen approximately never. Improvements that need breaking changes should happen under a new name, with a new project, so that the old project can still exist and receive updates.

I still say SemVer has done more harm than good for the industry... it normalized the idea that "it's ok to break compatibility, just bump the major version" and then you have libraries on version 26 that break everything every time the author decides to feng shuei the code some more. Every major version bump should be thought of as a sort of "grave marker" indicating a place in history where the author messed up and had to admit the old design was literally broken, and every downstream dependent had to go through some amount of pain as a result. It shouldn't be normalized.

You can say "nobody's forcing you to upgrade" but, with the amount of vulnerabilities out there, yes, you often are forced to upgrade certain dependencies.

You can say "just maintain the older versions too", and that would be absolutely great, if anyone really did this. In practice anything but the most recent major is neglected.

You can say "I agree and that's why I never release 1.0", and I'd say that's a reasonable approach, but that just changes the problem to "too many packages are pre-1.0" and we'd have the same issue.

You can say "that degree of compatibility is too difficult to expect out of open source maintainers", and my response is that there ought to be fewer open source maintainers. (For real. I'd rather have 1/100th the number of crates in Cargo, if the ones that were there were all maintained by people who were serious about backwards compatibility.)

You say "the API sucked and I hoped I could make it better", but the API is the library to anyone using it, and changing it means you're literally changing what the library is, which means all existing code using the library has to change to meet your new opinion on what the better API is (an opinion many wouldn't share, including and especially the original authors.) Why not just start a new project instead?

comment 1w ago · link · parent

The data on school shootings is very inconsistent. In most of the sources, a shooting between gangs that happens on a weekend at 2am that happens to be in school grounds is reported as a school shooting. So is any other murder between anyone on campus including staff, etc, even if they're not "mass" or "random" killings. Even suicide is often counted in these lists.

The closest analog we have for "person goes into a school while it's in session to kill people at random" is the FBI's list of "active shooter" incidents, and only some of those are counted as "Mass killings", and the data there says we've has as little as 3 (in 2024) and as many as 15 (in 2023) per year in the past 5 years: https://usafacts.org/articles/what-is-considered-a-mass-shoo...

And that 3-15 number is all mass killings, not just in schools (I don't know how many of those were in schools, but we could stipulate maybe 1 in 3?)

So, the 250-350 number is a couple of orders of magnitude higher than the type of school shootings parents are actually worried about when it comes to needing to text them during the day.

comment 1w ago · link · parent

How do you separate security advisories from regular updates? (If it’s just a box you check, it only means attackers will say “this is a security update” on the malicious package they upload.)

The point of cooldowns is for when a crate author’s account is compromised and used to publish malicious code. Saying “security advisories bypass cooldowns” wouldn’t really help anything there.

comment 1w ago · link · parent

There’s nothing special about primitives, they’re just types like any other. A method taking `&mut self` can always modify self, on any type. But you can only call such methods if the value is mutable to begin with, so it’s not really that interesting.

It’s only weird if you come from languages like Java where “primitives” are distinct from “objects”, which is a self-imposed limitation the language imposes.

comment 2w ago · link · parent

> I'd execute a chord or a melody as if it were a word or a sentence, without thinking about syntax

This is exactly the same for me. The guitar fretboard is essentially synesthesia for me when I hear music. I hear a melody and part of my brain feels the shape of the fingerings as I go. It’s been like this for maybe 30 years or so (I started guitar at a really young age.)

I have no idea if this is common for guitar players though (I assume so? If you’re the sort of player that can play solos, you probably already “audiate” sounds via fingering) but it means I can basically always immediately play a song after hearing it once, even if it’s just the basic melody. I also have what I’d call “weak” perfect pitch (I can easily conjure an E in my head and then figure out from there what note I’m hearing via well-trained relative pitch) so often I can get the exact notes right the first time.

12345
83