Page 1 of 1

Destructor?

PostPosted: Tue Jul 07, 2009 9:58 pm
by Caligari
(Can you tell I'm actually usig Cobra this week?)

Do we have destructors at the moment?

I've tried to use

Code: Select all
cue finalize

But I was told that Member "finalize" also exists in the case class. Cobra suggestion I might use "is override" (which I thought was one of the side-effects of using cue anyway), but when I did that, it told me that I was introducing a "finalize" method, which might interfere with a destructor, and then told me that rather than overriding object.Finalize I should provide a destructor...

Suggestions?

Re: Destructor?

PostPosted: Tue Jul 07, 2009 10:42 pm
by Charles
First, are you sure that you want a finalizer/destructor? The semantics can be complex. There is also IDisposable and the "using" statement.

Second, Cobra does not yet provide a cue for finalization even though it's planned.

Third, I confirm that the back-end C# compiler interferes with explicitly declaring a finalizer, apparently in an attempt to get you to use the right syntax. Of course, in this case, it's just getting in the way. So we need a "cue finalize" sooner rather than later.

Can you get by for now or is this a blocker?

Re: Destructor?

PostPosted: Tue Jul 07, 2009 11:00 pm
by Caligari
I have a class that is opening a file, and wanted to make sure it closed it when I was no longer using the instance. I can easily get by at the moment, though, as I'm only using this in one place, not across a bunch of applications.

Re: Destructor?

PostPosted: Tue Jul 07, 2009 11:48 pm
by Charles
The usual idiom for that is:
using obj = Class(args)
obj.foo
obj.bar

Where "Class" implements IDisposable. At the end of the "using" block, "obj.dispose" will be called. It's very similar to using try...finally.

In fact, files work like this (for any .NET language). See ReadAndWriteFiles.

I see some hits for IDisposable in the Cobra tests:
Code: Select all
505 ~/Projects/Cobra/Workspace-New/Tests $  gco idisposable
./120-classes/920-type-decl-clauses.cobra:44:class H inherits Object implements IDisposable has Test
./120-classes/920-type-decl-clauses.cobra:48:class I inherits Object implements IDisposable has Test is public
./120-classes/920-type-decl-clauses.cobra:52:class J inherits Object implements IDisposable, IEnumerable has Test, Test2 is public
./120-classes/920-type-decl-clauses.cobra:60:   implements IDisposable
./130-interfaces/202-error-use-implements-for-interfaces-in-decls.cobra:4:      inherits IDisposable
./130-interfaces/700-type-decl-clauses.cobra:43:interface H inherits A, IDisposable has Test
./130-interfaces/700-type-decl-clauses.cobra:46:interface I inherits A, IDisposable has Test is public
./130-interfaces/700-type-decl-clauses.cobra:49:interface J inherits A, IDisposable, IEnumerable has Test, Test2 is public
./130-interfaces/700-type-decl-clauses.cobra:53:        inherits A, IDisposable
./140-structs/300-type-decl-clauses.cobra:39:struct G is internal implements IDisposable has Test
./140-structs/300-type-decl-clauses.cobra:43:struct H implements IDisposable, IEnumerable has Test, Test2 is public
./140-structs/300-type-decl-clauses.cobra:50:   implements IDisposable


HTH