News for hackers. Stuff that matters.
Re:I am not yet ready to switch to Zig from Rust by ilrwbwrkhv on Friday June 14, 2024 @12:21PM (#40682156)
Go would be amazing if it didn't allow dumb mistakes such as:

    type Human struct {
        Name string
        MaybeHasCat *Cat
    }

    type Cat struct {
        Name string
    }

    func main() {
        h := Human{}
        h.MaybeHasCat.Name = "Taffy" 
    }
And boom. Null pointer exception because MaybeHasCat is null. The fact that go doesn't let you define an optional type is such a pain. A lot of newcomers while getting trained up fall for this bug.

edit: updating to add the pointer i missed. also adding a playground link: https://go.dev/play/p/izcod8xF7ZQ

Re:I am not yet ready to switch to Zig from Rust by koeng on Friday June 14, 2024 @12:26PM (#40682207)
That’s not true. Here is a go playground of pretty much that exact code running just fine

https://go.dev/play/p/v7OM9s_pRqY

Re:I am not yet ready to switch to Zig from Rust by deathanatos on Friday June 14, 2024 @12:51PM (#40682441)
Steelman their example. Given that they named the member "MaybeHasCat", it's pretty clearly meant to analogue an Option<Cat>; if you correct the small typo then (make the type of "MaybeHasCat" a pointer, so that it can store nil for the other half of the Maybe), the example behaves exactly as they describe.
Re:I am not yet ready to switch to Zig from Rust by iknowstuff on Tuesday June 18, 2024 @05:35AM (#40715702)
Wtf? what are you doing? Did the point fly over your head, are you being malicious by removing the star, or?
Re:I am not yet ready to switch to Zig from Rust by koeng on Wednesday June 19, 2024 @06:14PM (#40733003)
They added the star as an edit because of my playground link. So, neither
Re:I am not yet ready to switch to Zig from Rust by burnished on Friday June 14, 2024 @01:02PM (#40682551)
That would work. It would need to be a pointer to get a nil pointer exception, or an interface (obv function call to trigger instead of field access), but there are some simple and common patterns you would follow to prevent that.

Edit: should do more than just suggest the fix is easy

So for pointer types you have two options IMO: nil checks, or getters that do the nil check for you.

I think a map type instead of a string would also make your point more strongly because your zero value will fail on access. Which yeah that sucks to run into, and I'd agree actually that a lot of Go painpoints pop up early. I appreciate that now, because most of my foe stubbing was done inside the first few weeks, but it is undeniable imo.

Re:I am not yet ready to switch to Zig from Rust by ilrwbwrkhv on Friday June 14, 2024 @01:31PM (#40682831)
> nil checks, or getters that do the nil check for you

Yes I agree there are patterns to solve for this but it is just strange that the compiler doesn't stop you from making these errors. I mean even Typescript has:

  interface Human {
    name: string
    cat?: Cat
  }
Re:I am not yet ready to switch to Zig from Rust by burnished on Friday June 14, 2024 @01:52PM (#40683076)
I don't know what 'even typescript' is supposed to mean. Are you saying that typescript is the natural bar for comparison here? That doesn't seem correct, isn't typescript a project that tries to correct javascript's lack of typing?

Since it isn't an error to have a nillable type, and it isn't an error to access a field, it makes sense that the compiler doesn't error when you do that thing. If you want a warning when doing something silly then you can setup a linter.

If your first thought is that is not very beginner friendly then I agree with you - I edited my original response. But otherwise there are solutions to these problems.

If that doesn't suit you and Go isn't sweet enough for you, well, fair. Its rather austere. I don't think it is a categorical error for the project (though there are features I would also like for asking).

Re:I am not yet ready to switch to Zig from Rust by ilrwbwrkhv on Friday June 14, 2024 @02:20PM (#40683411)
My point is nillable values by default is a strange choice and throws out 50 years of computing history. It has been clearly established that it leads to null pointer exceptions. The fact that a modern language lets you do it is a bit strange.
Re:I am not yet ready to switch to Zig from Rust by burnished on Friday June 14, 2024 @03:36PM (#40684219)
Oh I didn't really get that from what you had been saying.

Sure, I'd agree it feels like a whoopsie, I dislike nil values because they are a common source of faults.

Re:I am not yet ready to switch to Zig from Rust by matrix87 on Friday June 14, 2024 @12:27PM (#40682215)
Could just do null -> none, nonnull -> some, optional doesn't introduce anything new here. Either you null check or you optional check

Optionals are just another outlet for "best practice" bullshittery and not much else. If go doesn't have, sounds like they're following YAGNI principle

Re:I am not yet ready to switch to Zig from Rust by ilrwbwrkhv on Friday June 14, 2024 @03:36PM (#40684218)
> Could just do null -> none, nonnull -> some, optional doesn't introduce anything new here. Either you null check or you optional check

I am not sure what you mean by the first point but what I am saying is that the compiler doesn't stop you from having a basic runtime null pointer exception because of "simplicity" and "yagni".

Re:I am not yet ready to switch to Zig from Rust by archgoon on Friday June 14, 2024 @12:29PM (#40682236)
I think you meant

  type Human struct {
     Name string
     MaybeHasCat *Cat
  }
As you have written (not making MaybeHasCat a pointer), go will correctly initialize (at least as of go1.20.5) the nested Cat structure and allow for direct assignment.

Having a null pointer exception for a null pointer is a behavior I would expect. Having one for a non-pointer structure would be surprising.