[HN Gopher] Cursed Linear Types in Rust
       ___________________________________________________________________
        
       Cursed Linear Types in Rust
        
       Author : todsacerdoti
       Score  : 62 points
       Date   : 2024-11-27 10:14 UTC (4 days ago)
        
 (HTM) web link (geo-ant.github.io)
 (TXT) w3m dump (geo-ant.github.io)
        
       | MrMcCall wrote:
       | The problem is that programming languages have _always_ focused
       | on the definition side of types, which is absolutely necessary
       | and good, but the problem is that only limiting use by, e.g.,
       | "protected, private, friend, internal, ..." on class members, as
       | well as the complicated ways we can limit inheritance, are barely
       | useful.
       | 
       | We need a way to define how to "use" the types we define. That
       | definitional structure is going to bleed from creation of
       | instances into how they live out their lifetimes. It appears that
       | Rust's design addresses some aspects of this dimension, and it
       | also appears to be a fairly major point of contention among
       | y'all, or at least require a steepish learning curve. I don't
       | know, as I prefer to work in ubiquitous environments that are
       | already featureful on 5-10yo distros.
       | 
       | One usage pattern many of us have found useful in our software
       | for years is the "set once and only once" (singleton-ish) ,
       | whether it's for a static class member or a static function var,
       | or even a db table's row(s). I don't know of any programming
       | environment that facilitates properly specifying calculating
       | something even that basic in the init phase of running the
       | system, but I don't explore new languages so much anymore, none
       | of them being mature enough to rely upon. Zig's comptime stuff
       | looks promising, but I'm not ready to jump onto that boat just
       | yet. I am, however, open to suggestions.
       | 
       | The real solution will ultimately require a more "wholistic"
       | (malapropism intended) approach to constraining all dimensions of
       | our software systems while we are building them out.
        
         | logicchains wrote:
         | >One usage pattern many of us have found useful in our software
         | for years is the "set once and only once" (singleton-ish)
         | 
         | C# has this: https://learn.microsoft.com/en-
         | us/dotnet/csharp/language-ref...
        
           | cempaka wrote:
           | And Java will soon have StableValue:
           | https://openjdk.org/jeps/8312611
        
             | neonsunset wrote:
             | Reading the JEP, isn't StableValue about JIT constants?
             | 
             | In .NET it's just 'static readonly'.
        
             | mrkeen wrote:
             | Non-goals       It is not a goal to provide Java language
             | support for declaring stable values.
             | 
             | Hmmm
        
               | jcrites wrote:
               | From context, I would infer that this means they are not
               | changing the Java language itself. It's a feature
               | expressed through the existing language, like as a
               | library feature. I could be wrong though.
        
           | MrMcCall wrote:
           | That's new since I've been tramping around VS.NET (F# 2 or 3
           | & C# from 10ya).
           | 
           | My immediate purposes require that I avoid depending on
           | unique programming language/environment constructions, but it
           | helps to learn, so thanks for levelling me up.
        
           | chipdart wrote:
           | > C# has this:
           | 
           | This is only syntactic sugar to allow using object
           | initializers to initialize specific member varabiles of a
           | class instance instead of simply using a constructor and/or
           | setting member variables in follow-up statements. It's hardly
           | the feature OP was describing.
        
             | neonsunset wrote:
             | While mutation of init-only properties is sometimes done by
             | e.g. serializers through private reflection or unsafe
             | accessors, it otherwise can lead to unsound behavior. You
             | cannot bypass this through normal language means.
             | 
             | Same applies to readonly instance fields.
             | 
             | Where does "syntax sugar" end and "true features" begin?
        
         | pornel wrote:
         | Rust's exclusive ownership can be used for things that can be
         | called at most once (a method can require a non-copyable object
         | as an argument, and takes ownership of it away from you, so you
         | can't call the method again).
         | 
         | Rust also has wrapper types like OnceCell that have
         | `get_or_init()` which will init the thing only once, and then
         | it's guaranteed to be immutable. Unlike singletons, these don't
         | have to be globally accessible.
        
         | chipdart wrote:
         | > The problem is that programming languages have always focused
         | on the definition side of types, which is absolutely necessary
         | and good, but the problem is that only limiting use by, e.g.,
         | "protected, private, friend, internal, ..." on class members,
         | as well as the complicated ways we can limit inheritance, are
         | barely useful.
         | 
         | Virtually all software ever developed managed just fine to with
         | that alone.
         | 
         | > I don't know of any programming environment that facilitates
         | properly specifying calculating something even that basic in
         | the init phase of running the system, (...)
         | 
         | I don't know what I'm missing, but it sounds like you're
         | describing the constructor of a static object whose class only
         | provides const/getter methods.
         | 
         | > or even a db table's row(s).
         | 
         | I don't think you're describing programming language
         | constructs. This sounds like a framework feature that can be
         | implemented with basic inversion of control.
        
       ___________________________________________________________________
       (page generated 2024-12-01 23:00 UTC)