improvements to import form

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

improvements to import form

Per Bothner
Using define-alias to abbreviate a classname is a bit verbose:

(define-alias StringBuffer java.lang.StringBuffer)

I checked in a new variant of import which should be more convenient:

(import (class java.lang StringBuffer))

The syntax is (import (class PREFIX NAME1 NAME2 ... NAMEN)).
This is roughly equivalent to:
   (define-alias NAME1 PREFIX.NAME1)
   (define-alias NAME2 PREFIX.NAME2)
   ...
   (define-alias NAMEN PREFIX.NAMEN)

Strictly speaking, it's more like define-private-alias, since the new
names are (by default) not exported.

You can do renaming; you can have multiple class-clauses in an import;
you can combine it with other import clauses (most usually prefix).

As an example, kawa/lib/numbers.scm now contains:

(import (class java.lang Double)
         (class gnu.math IntNum Numeric RatNum RealNum (Quaternion quaternion))
         (class gnu.kawa.lispexpr LangObjType))

Note how gnu.math.Quaternion is renamed to quaternion.

An example showing the use with prefix:

(import (prefix (class java.lang Long Short) jl-))

This is equivalent to:

(import (class java.lang (Long jl-Long) (Short jl-Short)))

which is equivalent to:

(define-private-alias jl-Short java.lang.Short)
(define-private-alias jl-Long java.lang.Long)

In a related change, I extended the only clause of the import form to
allow reaming, having having to write an explicit rename clause.  For example:

(import (only (kawa example) A (B1 B2) C (D1 D2)))

is equivalent to:

(import (rename (only (kawa example) A B1 C D1)
                 (B1 B2) (D1 D2)))

The names A, B1, C, and D1 must existing in the library (kawa example).
The bindings are accessible using the names A, B2, C, and D2.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: improvements to import form

mikel evins

> On Apr 15, 2015, at 1:07 AM, Per Bothner <[hidden email]> wrote:
>
> Using define-alias to abbreviate a classname is a bit verbose:
>
> (define-alias StringBuffer java.lang.StringBuffer)
>
> I checked in a new variant of import which should be more convenient:
>
> (import (class java.lang StringBuffer))
>
> The syntax is (import (class PREFIX NAME1 NAME2 ... NAMEN)).
> This is roughly equivalent to:
>  (define-alias NAME1 PREFIX.NAME1)
>  (define-alias NAME2 PREFIX.NAME2)
>  ...
>  (define-alias NAMEN PREFIX.NAMEN)
>
> Strictly speaking, it's more like define-private-alias, since the new
> names are (by default) not exported.

Thanks, Per. This is very nice for my uses and will eliminate some macrology I've been using.