This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to define constructors for classes defined usingdefine-simple-class


Hi All,

On Wed, 2005-03-30 at 15:58 -0800, Per Bothner wrote:
> It's partly a design issue.  Any design will be a bit of a kludge:
> what's a "constructor"?  It's a special kind of beat: neither a
> normal instance method nor a static method.  I think having constructors
> in a language is a mistake - one should use factory methods instead.
> 
> What I think we'll do is define "constructors" as if they were methods
> with a special name - perhaps "new", and a few special rules.

I've had a go hacking this up a few months ago. The approach I took was
to munge methods with the name "new" to "<init>" and then in the code
generation for methods, if it came across an <init> method it would
output the necessary code to call $finit$ and the superclass <init>.
There are lots of limitations: it only calls the 0-argument superclass
<init> (because I haven't written support to try all the suitable
superclass <init>'s in the hierarchy); you can't create/override 0-
argument constructors in your new class (I think this is because the 0-
arg <init> has already been created by the method code-generation time);
there's no support for programmer-specified superclass constructor
calls. And probably many, many more problems... I'll try to have another
look at this in the coming weeks.
Would anyone be interested in a partial solution? Or, better still,
would someone be willing to fix up my code?!

> Support for calling a super-constrcutor can be based on the existing
> invoke-special.

Is there a simple way to check whether the new/<init> method contains a
"super" call at code-generation time? I was considering a lazy approach
to this where "new" would get converted into a constructor with default
superclass <init> and "new-no-super" would get converted into a
constructor where the programmer has to call invoke-special manually.

Kind regards,

David


Attached is an example class (not using inheritance in this case).
(define-simple-class <TestClass> ()
    (val :: <int> init-value: -1)
    
    ;; 1-arg constructor
    ((new v :: <int>) :: <void>
    	(begin
	    (set! val v)
            (display "My 1-arg constructor works!")
            (newline)
        )
    )

    ;; 0-arg constructor
    ((new) :: <void>
	(begin
	    (set! val 1)
            (display "My 0-arg constructor works!")
            (newline)
        )
    )

    ((to-string) (as <String> "TestClass instance"))
    ((get-val) val)
)

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]