This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
extended "colon notation"
- From: Per Bothner <per at bothner dot com>
- To: kawa at sources dot redhat dot com
- Date: Fri, 29 Jul 2005 15:26:56 -0700
- Subject: extended "colon notation"
I've gotten patches (form Merced Systems) to extend Kawa's "colon
notation" making use of namespace prefixes for method access. E.g. we
can currently do this like:
(define-namespace SBuf <java.lang.Stringbuffer>
(SBuf:append (SBuf:new) 12)
The patch adds support for:
(SBuf:instanceof? x)
;; equivalent to (instance? x <java.lang.Stringbuffer>)
(SBuf:cast x)
;; equivalent to (as <java.lang.Stringbuffer> x)
(SBuf:class)
;; equialent to (java.lang.class:forName "java.lang.Stringbuffer")
I have a couple of reservations. First, "cast" is a valid Java
identifier, so there might be Java method named "cast". We can make
sure to first search for a method named "cast", and only apply the above
meaning if we don't find such a method. However, that is a bit awkward.
It might be better to use a symbol that is not a valid Java name.
Perhaps: (SBuf:-> x) or (SBuf:<- x) to mimic Scheme functions like
string->list.
For :class note that Java uses the StringBuffer.class syntax - i.e. it
uses the same syntax as static field access. Perhaps we should first
consider how we'd extend the colon notation to fields? One idea is to
require a dot. For example: (java.lang.Integer:.MAX_VALUE)
[Note one might also say that field access from Scheme is like a
variable access rather than a function call, so you shouldn't need the
parentheses: java.lang.Integer:.MAX_VALUE
However, to handle instance fields, it appears we need the parentheses:
(gnu.lists.Pair:car x)
So for consistency we should have the parentheses for static field
accesses as well.]
In that case, perhaps the syntax should be: (SBuf:.class) [with a dot]
instead? Of course we can allow both syntaxes, since "class" is a
reserved indentifier in Java.
A final extension might be to allow the class name to be left out when
the method is an instance method or the field is an instance field,
since in that case it can be inferred from the first parameter:
(:toString x)
Unfortunately, this clashes with the (alternative) keyword syntax, since
(at least currently) :toString is the same as toString: If we want to
allow this syntax (for compatibility with other Schemes and Common Lisp,
if nothing else), then it becomes difficult to use the :toString syntax.
However, we can use a special prefix character: (*:toString x)
or: (.:toString x)
or: (_:toString x)
[The latter is technically ambiguous, since "_" is a valid Java identifier.]
Accessing an instance field would be similar: (*:.re z)
or: (.:.re z)
or: (_:.re z)
Comments?
--
--Per Bothner
per@bothner.com http://per.bothner.com/