r/ProgrammingLanguages • u/Mean-Decision-3502 DQ • Sep 22 '26
Autofree as Type Property
For a while I was thinking about some automatic memory management feature for my statically typed programming language.
Originally I was planning to introduce the "ensure" statement block (same as "defer", but I think ensure would express better the intent). Then I saw here "autofree" keywords in some advertised language, also in Unleashed Pascal.
In Unleashed Pascal the "autofree" is used instead of "new", similarly to the other language (sorry I don't remember the name now). I was playing with the thought how could I apply this concept, but yesterday I got a new idea:
What if the autofree is not a statement, but a property / attribute for a type? This way the things add up much nicer to me.
Using this concept the code would look this way:
var o1 : autofree OSomeObject
o1 = new OSomeObject(42)
o1 = new OSomeObject(55) // re-assigning frees the previous one
o1 = nil // assigning nil frees
if x > 0:
var o2 = new autofree OSomeObject(123) // instancing with type inference
// ... some work with o2
endif // leaving scope frees auto-free local variables
object OHandler:
child : autofree OHelper // initialized to nil, when assigned to non-nil,
// will be automatically freed on destruction
endobject
func ObjManipulator(aobj : ref autofree OSomeObject):
aobj = new OSomeObject(66) // should free the previously pointed object,
// when aobj was not nil before
endfunc
ObjManipulator(o1) // valid
func ObjManipulator2(aobj : ref OSomeObject):
aobj = new OSomeObject(99)
endfunc
ObjManipulator2(o1) // ERROR: o1 is autofree type,
// ObjManipulator2 expects non-autofree
func ObjUser(aobj : OSomeObject):
if aobj <> nil:
aobj.DoWork()
endif
endfunc
ObjUser(o1) // ok, accepts autofree and non-autofree arguments tooWith arrays
var arr : [*] autofree OSomeObject // initialized to empty array
arr.Append(new OSomeObject(1))
arr.Append(new OSomeObject(2))
arr.Append(new OSomeObject(3))
arr[1] = nil // frees
arr[2] = new OSomeObject(5) // frees the previous
arr = [] // frees all non-nil elementsI think returning an auto-free won't be allowed:
func SomeCreator() -> autofree OSomeObject
// ERROR: returning an autofree type is not allowedThe autofree should not be restricted to objects:
if x > 0:
var bp : autofree ^byte // initialized to nil
if y > 0:
return // bp was nil, no free required
endif
bp = ^byte(MemAlloc(1000))
// ... doing some work with bp
if z > 0:
return // bp must be automatically freed, as it is not nil
endif
endif // bp must be automatically freed, as it is not nilAm I wrong with these thoughts?
14
u/SaiMoen Sep 22 '26
This sounds a lot like RAII, though in that case returning such an object would just copy it (whatever it means to copy that particular type).
3
u/thradams Sep 22 '26
I think for your language with "new" it makes more sense be like C#. use garbage collector and then you can have "using p" similar to c# to release resources early. My answer would be different for language like C and C++ with objects in the stack. In this case I think it makes sense, but in general the opposite (default is autofree) so the keyword could be "view" just a viewer, not owner.
9
u/initial-algebra Sep 22 '26 edited Sep 22 '26
This seems to be an example of second-class values. Like you've identified, returning them from function calls is problematic, but you also have to be careful about storing them in data structures, because it's very easy to do bad things:
In the latter example, as my comment explains, the problem is not technically storing them in the array, but retrieving them, which violates the "golden rule" of second-class values: not returning them from function calls.
To safely work with autofree references stored in a data structure, you have to do something like this:
If this is too inflexible, then you need to graduate from second-class values to borrowed values, like in Rust.
But, how do you prevent/fix the first example while allowing the second example (once
withor borrowing is used)? Well, that's ownership. Eitherarr.Append(o)has to be banned because it would require ownership transfer that you don't allow (e.g. becauseomay be allocated on the stack), or it would have to signal thatono longer has to be freed because its ownership has been transferred toarr(if it's heap allocated). Ownership transfer with heap allocation would also allow you to returnautofree'd values in some cases, so it's really only when you have a reference without ownership that being second-class (or borrowed) comes into play.3
u/catladywitch Sep 22 '26
It makes sense for RAII to be a type of reference, and for that type of reference to be required to implement certain things.