Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] New auto operator.


Some thoughts.

---

My first concern was that I might want to dispose the same type in
different ways (sometimes a few fields of a struct should be free'd,
sometimes not).

This works pretty seamlessly with types: here's a free test :)

        use std
        use testr
        
        type foo = struct
                x : int
                y : byte[:]
        ;;
        
        type bar = foo
        
        var freed : int = 0
        impl disposable foo =
                __dispose__ = {a; std.slfree(a.y); freed++ }
        ;;
        
        impl disposable bar =
                __dispose__ = {a; }
        ;;
        
        const main = {
                testr.run([
                        [.name = "types-01", .fn = types01],
                ][:])
        }
        
        const types01 = {c
                freed = 0
                doit();
                testr.check(c, freed == 1, "expected b.y to be freed")
        }
        
        const doit = {
                var a : bar = auto [.x = 1, .y = "abc"]
                var b : foo = auto [.x = 2, .y = std.sldup("def")]
                var c : bar =      [.x = 3, .y = "qrt"]
                var d : foo =      [.x = 4, .y = "mno"]
        }

---

There seem to be some rough edges with types; the following crashes
6m for me:

        use std
        
        impl disposable byte[:] =
                __dispose__ = {s; std.put("disp “{}”\n", s) }
        ;;
        
        const main = {
                var a : byte[:] = auto "foo"
        }

---

The same seems to happen if I try to do silly things with generics.

        use std
        
        type foo(@a) = struct
                s : @a[:]
                i : std.size
        ;;
        
        impl disposable foo(@a) =
                __dispose__ = {f; }
        ;;
        
        generic f : (x : @a -> foo(@a)) = {x
                -> [.s = std.sldup([x][:]), .i = 0]
        }
        
        const main = {
                var g = auto f(1)
        }

---

As I mentioned on IRC, I don't think the name "auto" is as clear
as it could be. I blame C++ for polluting the definition space.  It
feels petty to mention, I won't push if people think "auto" is fine.

---

One thing I can foresee is that new users might think this is full
GC, and expect things like "-> auto f(x, y)" to work. That might
be something to emphasize in the documentation.

---

Anyway, I'm really looking forward to this personally. For example,
one of my pain points right now is that iterators with lots of
internal state are difficult to use without leaking memory, and
this will fix it right up.

Thanks,
S. Gilles

References:
[PATCH] New auto operator.Quentin Carbonneaux <quentin@xxxxxx>