-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
free is always an override in classes #129
Comments
Why is |
Because you can remove it entirely and get the exact same result - the parent's implementation will be used instead (that's how override works). |
Any ETA on this? |
Note that not all code paths have to do the super call. |
Can you name any object, except |
Derived class destructor that does not have to call the base class destructor ? This is not even possible in C++ :) |
Free might not always deallocate the object. That might be done in other ways. |
Okay, fine. |
I think it could be easier if tl:dr: make a basic OK, so this is what I think about it. I don't expect you to agree. In fact I'm pretty sure you won't :) But really, the fact that you can omit the base class destructor in derived is kinda scary. Does this come from C# as well ? I think it is even worse than throwing exceptions from destructors :/ |
I agree that If one needs to have a It is however not a very good abstraction around I don't agree on the other hand that it is wrong to use In the end I just would like to remind you that in ooc neither new nor free/delete are operators. They are just methods like other methods. Throwing exceptions from destructors in C++ leaks memory while this does not do that. In the end I think it is your C++ reflexes that are kicking in. I should add that in C# one has to implement it the way you suggest but in ooc it is not only possible but also encouraged in the documentation: (from https://ooc-lang.org/docs/lang/classes/) Dog: class {
pool := static Stack<This> new()
new: static func -> This {
if (pool empty?()) {
obj := This alloc()
obj __defaults__()
obj
} else {
pool pop()
}
}
free: func {
pool push(this)
}
} But in the end I must just state for the record that I really dislike the |
Yes it is completely different design and thinking in ooc. Not worse or better, just different. |
|
Yes, but I meant that it can be more loosely coupled, by moving the // Recyclable.ooc
Recyclable: class <T> {
_lock := static Mutex new()
_smallRecycleBin := static VectorList<T> new()
_object: T = null
...
new: static func ~fromSize (size: Int) -> This {
bin := This _getBin(size)
This _lock lock()
for (i in 0 .. bin count) {
if ((bin[i] size) == size) {
// search for free object in bin and store in 'this _object '
}
}
This _lock unlock()
this _object
}
free: override func {
if (this _object)
putToBin(this _object)
super()
}
}
// etc. for other reusable types
Then you could have the buffer := ByteBuffer new(100) // ordinary byte buffer
buffer := Recyclable<ByteBuffer> new(100) // reusable one
vector := VectorList<Float> new()
vector := Recyclable<VectorList<Float>> new()
matrix := FloatMatrix new()
matrix := Recyclable<FloatMatrix> new()
// etc ...
I don't know the details of implementation yet, this is just an idea. Btw. in ooc it is easier to implement thanks to the overriden template<typename T>
class Reusable : public T {
}; The so called "curiously recurring template pattern" (a kind of). I wonder it it's even possible in ooc Test: class <T> extends T {
} Anyway, now we have |
We did do something in this direction in https://github.com/cogneco/Kean/tree/develop/Kean/Recycle. |
For any class in other than
Object
, this is a valid implementation of free in a class:This is not:
and neither is this:
Also, this is useless:
Magic should be able to detect when a class has an invalid
free
function, but it will require that magic knows it's looking at a class, and not a cover, because those rules don't apply there.The text was updated successfully, but these errors were encountered: