• 0 Posts
  • 97 Comments
Joined 1 year ago
cake
Cake day: July 31st, 2023

help-circle



  • That’s not the point, though. The point is to use a nominal type that asserts an invariant and make it impossible to create an instance of said type which violates the invariant.

    Both validation functions and refinement types put the onus on the caller to ensure they’re not passing invalid data around, but only refinement types can guarantee it. Humans are fallible, and it’s easy to accidentally forget to put a check_if_valid() function somewhere or assume that some function earlier in the call stack did it for you.

    With smart constructors and refinement types, the developer literally can’t pass an unvalidated type downstream by accident.


  • You’re going to need to cite that.

    I’m not familiar with C23 or many of the compiler-specific extensions, but in all the previous versions I worked with, there is no type visibility other than “fully exposed” or opaque and dangerous (void*).

    You could try wrapping your Foo in

    typedef struct {
        Foo validated
    } ValidFoo;
    

    But nothing stops someone from being an idiot about it and constructing it by hand:

    ValidFoo trustMeBro;
    trustMeBro.validated = someFoo;
    otherFunction(trustMeBro);
    

    Or even just casting it.

    Foo* someFoo;
    otherFunction((ValidFoo*) someFoo);
    

  • If it were poorly designed and used exceptions, yes. The correct way to design smart constructors is to not actually use a constructor directly but instead use a static method that forces the caller to handle both cases (or explicitly ignore the failure case). The static method would have a return type that either indicates “success and here’s the refined type” or “error and this is why.”

    In Rust terminology, that would be a Result<T, Error>.

    For Go, it would be (*RefinedType, error) (where dereferencing the first value without checking it would be at your own peril).

    C++ would look similar to Rust, but it doesn’t come as part of the standard library last I checked.

    C doesn’t have the language-level features to be able to do this. You can’t make a refined type that’s accessible as a type while also making it impossible to construct arbitrarily.


  • Unless you’re a functional programming purist or coming from a systems programming background, it takes a lot longer than a few days to get used to the borrow checker. If you’re coming as someone who most often uses garbage-collected languages, it’s even worse.

    The problem isn’t so much understanding what the compiler is bitching about, as it is understanding why the paradigm you used isn’t safe and learning how to structure your code differently. That part takes the longest and only really starts to become easier when you learn to stop fighting the language.




  • The first directory block is a hole. But type == DIRENT, so no error is reported. After that, we get a directory block without ‘.’ and ‘…’ but with a valid dentry. This may cause some code that relies on dot or dotdot (such as make_indexed_dir()) to crash

    The problem isn’t that the block is a hole. It’s that the downstream function expects the directory block to contain . and .., and it gets given one without because of incorrect error handling.

    You can encode the invariant of “has dot and dot dot” using a refinement type and smart constructor. The refined type would be a directory block with a guarantee it meets that invariant, and an instance of it could only be created through a function that validates the invariant. If the invariant is met, you get the refined type. If it isn’t, you only get an error.

    This doesn’t work in C, but in languages with stricter type systems, refinement types are a huge advantage.






  • Part of the hostility was the other maintainer misunderstanding the presenter, going on a diatribe about how the kernel Rust maintainers are going to force the C code to become unrefactorable and stagnate, and rudely interrupting the presenter with another tangent whenever he (the presenter) tried to clarify anything.

    An unpleasant mix of DM railroading and gish galloping, essentially.

    I wouldn’t quite call it a strawman, but the guy was clearly not engaging in good faith. He made up hypothetical scenarios that nobody asked about, and then denigrated Rust by attacking the scenarios he came up with.

    Edit: I was thinking of the wrong fallacy. It is a strawman, yes.






  • I’d be great if the block would make them unable to downvote you and your posts as well. And it’d be nice if it wouldn’t even let them reply to your posts.

    I’m not entirely sure that’s going to work out the way people think it will.

    Suppose I’m some jackass that gets off on harassing you: if blocks prevented me from interacting with your content, and you blocked me, I would have confirmation that I’ve successfully gotten under your skin. I can then just make another account and continue what I’m doing.

    If blocks don’t notify or provide indication to the blocked party, they would either escalate their behavior (while you are blissfully unaware) and get banned by a moderator, or give up and move on to someone else.

    There’s also considering how that’s going to work with moderators and admins: do they get to bypass the block and continue to comment and interact with you against your wishes? Does it hide your posts from them if they’re blocked? It’s a lot harder to design this type of blocking on a community-centric platform than it is to do for a microblogging platform like Twitter or Tumblr.

    Because muting doesn’t stop the poison the spread, just my personal ability to not see it.

    That’s what mods and admins are supposed to do. It’s not the users’ responsibility to moderate the behavior of others, and it’s a lot less stressful than trying to stop toxicity when you only have words in your moderator toolbox.