public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* how to define/put into Environment with type hints?
@ 2017-09-20 12:36 Sonny To
  2017-09-20 13:17 ` Per Bothner
  0 siblings, 1 reply; 2+ messages in thread
From: Sonny To @ 2017-09-20 12:36 UTC (permalink / raw)
  To: Kawa mailing list

I tried something like this

env.put(Symbol.valueOf("application"), Symbol.makeUninterned("::
android.app.Application"),application)

the result I want is
(define application :: android.app.Application application)


if I do, env.put(Symbol.valueOf("application"),application) accessing
application from the repl will
give warnings about missing symbols because its treated as an
java.lang.Object. I don't want to explicitly cast every time i want to
invoke a method or property of application

thanks,
Sonny

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: how to define/put into Environment with type hints?
  2017-09-20 12:36 how to define/put into Environment with type hints? Sonny To
@ 2017-09-20 13:17 ` Per Bothner
  0 siblings, 0 replies; 2+ messages in thread
From: Per Bothner @ 2017-09-20 13:17 UTC (permalink / raw)
  To: Sonny To, Kawa mailing list

On 09/20/2017 05:36 AM, Sonny To wrote:
> I tried something like this
> 
> env.put(Symbol.valueOf("application"), Symbol.makeUninterned("::
> android.app.Application"),application)
> 
> the result I want is
> (define application :: android.app.Application application)
> 
> 
> if I do, env.put(Symbol.valueOf("application"),application) accessing
> application from the repl will
> give warnings about missing symbols because its treated as an
> java.lang.Object. I don't want to explicitly cast every time i want to
> invoke a method or property of application

It is common for people new to Kawa to want to play around with
environments and/or eval.  My general advise to minimize doing either.
Scheme is built around lexical binding, and modern Schemes take that
further using modules/libraries. Kawa's strength is compilation including
compile-time analysis.

So if you can possibly avoid using environment objects or eval, do so.

There is no way to directly specify types with an environment binding.
However, it is possible to do it indirectly:

To get the effect of:
   (define application :: android.app.Application app)
you need to evaluate or compile that.

The resulting class can be imported (using import/require),
and the resulting bindings will have the type specifier.

The last piece of the puzzle is loadClass, which loads all
the public bindings in a class into an environment.

You can use either: kawa.standard.Scheme.loadClass
or the lower-level: gnu.language.LoadClass
or the still lower-level: gnu.kawa.reflect.ClassMemberLocation.defineAll

It might be reasonable to define a helper procedure:
   (environment-import clas [environment])
This would be a simple wrapper around ClassMemberLocation.defineAll.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-09-20 13:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-20 12:36 how to define/put into Environment with type hints? Sonny To
2017-09-20 13:17 ` Per Bothner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).