This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: type declaration best practices?
- From: Per Bothner <per at bothner dot com>
- To: kawa at sourceware dot org
- Date: Sun, 10 Jan 2010 22:29:32 -0800
- Subject: Re: type declaration best practices?
- References: <FF51B6B0-46D4-4C37-B521-831014EB4B73@theptrgroup.com> <4AFCA0F9.3060300@bothner.com> <m2my2q3nho.fsf@gmail.com> <4AFD109E.7090307@bothner.com>
I changed (in SVN) the Kawa reader so white space
are optional both before and after :: .
Thus you can write:
(define var1 ::int 12)
or:
(define var2::list '(3))
I did try changing the reader so that
:: TYPE
would be read as
($coloncolon$ TYPE)
However, this turned out to be a lot of work. It might make
sense if re-doing Kawa from scratch, but since we need to support
the old syntax, it was a lot easier to just tweak the reader.
Given that we're more-or-less stuck with :: what would be
the preferred type declaration syntax? For variable declarations
I think I would recommend keeping the space before the colon,
but leaving it out after, as in:
(define var1 ::int 10)
(let ((var2 ::list (cons x y))) ...)
For function declarations, I'm dithering between:
(1)
(define (string-ref (str ::string) (k ::int)) ::char
(invoke str 'charAt k))
(2)
(define (string-ref str ::string k ::int) ::char
(invoke string 'charAt k))
(3)
(define (string-ref str::string k::int) ::char
(invoke string 'charAt k))
(1) is more verbose, but it may be easier to read (for
humans) and it handles default values (for #!optional
parameters) better.
Comments?
So how would (future) type annotations fit in? I think by
coming after the :: as in:
(define x ::(@NonNull)string (...))
[This is an advantage of having :: as just a plain symbol
- it makes it easier to add type annotations - at least
conceptually.]
Note above I'm talking about type annotations, as are likely
to be added to jdk7. The Java5 declaration annotations would
go someplace else, possibly before the variable name, as in:
(define (@Deprecated) bar #!null)
Advantage of this is that it is matches Java, and that it
works with function definitions, as in:
(define (@Override) (foo ...) ...)
But this is to be decided later.
--
--Per Bothner
per@bothner.com http://per.bothner.com/