private(this) vs LINT

forkit forkit at gmail.com
Tue Jun 28 03:52:08 UTC 2022


On Monday, 27 June 2022 at 20:23:26 UTC, Daniel N wrote:
> I was just thinking, if we had a standard linter, it could 
> solve many issues without bloating the language, since the 
> compiler exists as a library it should not be too difficult to 
> maintain either.
>
> This way we can both keep the cake and eat it.

I'm curious how a linter would have helped me here
(see the comment I had to make ***to myself** in the unittest 
section)

// ---
module test;

class unsafeVector
{
   private:
     size_t *elem;
     immutable size_t sz;

   public:
     this(size_t s)
     {
         elem = cast(size_t*)new size_t[s];
         sz = s;
     }

     auto ref opIndex(size_t i)
     {
         return elem[i];
     }

     auto ref opSlice(size_t start, size_t end)
     {
         return elem[start .. end];
     }

     auto ref opDollar()
     {
         return sz;
     }

     auto size()
     {
         return sz;
     }
}

// compile with: -unittest -main
unittest
{
     import std;

     unsafeVector v = new unsafeVector(5);

     // Initialise each element with a different value.
     // NOTE TO SELF: Be sure to use the public interface here.
     // Don't accidently use v.sz like I did intially.
     for (int i = 0; i < v.size(); i++)
     {
         v[i] = i+1;
     }

     assert(v[0..3] == [1,2,3] );
     assert(v[0..$] == [1,2,3,4,5]);
     assert(v[$-1] == 5);

     v[2] = 999;
     assert(v[2] == 999);
}

//-----



More information about the Digitalmars-d mailing list