This is the mail archive of the kawa@sourceware.org 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: Calling getClass() on a Scheme subclassed object


Sean Luke wrote:
> After some further reading on the list, I've seen (invoke <Foo>
> 'getReflectClass), which returns
>     class Foo

I believe that you are indeed getting a java.lang.Class back, but you
can verify this by seeing what

  (*:getClass (invoke <Foo> 'getReflectClass))

returns.

> That looked lots more promising, until I actually tried using it:
> 
> (<sim.engine.SimState>:doLoop (invoke <Foo> 'getReflectClass)
> (<java.lang.String[]> (*:toString "-until") (*:toString 5000))
> 
> MASON Version 12.  For further options, try adding ' -help' at end.
> Job: 0 Seed: 1148961645567
> Starting Foo
> Invalid parameter, was: Foo
> java.lang.ClassCastException: Foo
>         at ca.step(ca.scm:11)
>         at sim.engine.Repeat.step(Schedule.java:497)
>         at sim.engine.Schedule.step(Schedule.java:219)
>         at sim.engine.SimState.doLoop(SimState.java:483)
>         at sim.engine.SimState.doLoop(SimState.java:294)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 
>         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 
>         at java.lang.reflect.Method.invoke(Method.java:585)
>         at gnu.expr.PrimProcedure.apply(PrimProcedure.java:238)
>         at gnu.mapping.CallContext.runUntilDone(CallContext.java:251)
>         at gnu.expr.ModuleExp.evalModule(ModuleExp.java:268)
>         at kawa.Shell.run(Shell.java:230)
>         at kawa.Shell.run(Shell.java:172)
>         at kawa.Shell.run(Shell.java:159)
>         at kawa.repl.main(repl.java:846)
> 
> The doLoop method constructs a Foo using reflection, and ultimately
> passes the instance to ca.step (ca is another Scheme simple-class).  In
> the step method we see:
> 
> (define-simple-class <ca> (<sim.engine.Steppable>)
>     ((step (state :: <sim.engine.SimState>)) :: <void>
>     (let ((tut :: <Foo> (as <Foo> state)) ...
> 
> So step is being handed an instance of whatever class was provided in
> (invoke <Foo> 'getReflectClass), and it's now trying to cast that
> instance into a <Foo>.  That *ought* to work but it doesn't.

The fragment of step you're showing looks like it should work to me,
too.  I presume one of those lines is line 11 of ca.scm?

Some thoughts:  Maybe the bytecode being generated for the class is
incorrect; how about instantiating an instance of <Foo> yourself and
seeing if it behaves properly.  Maybe the problem is earlier in the
call tree; how about outputting the <Foo> instance information and any
other pertinent info at sim.engine.Repeat.step(Schedule.java:497)
before calling ca.step(ca.scm:11) to see what's there.

Dean

> Sean
> 
> On May 29, 2006, at 11:52 PM, Sean Luke wrote:
> 
>> I maintain a simulation toolkit (MASON) in Java and have been
>> interested in seeing if I can get some simulation apps written in
>> Scheme rather than in raw Java.  So far I've been successful, and
>> after heavily typing the scheme code, it runs at very close (about
>> 1.5) the speed of my Java code.  Not bad!
>>
>> Unfortunately I'm stuck with a difficulty.  In the system you
>> typically create a subclass of a Java class (sim.engine.SimState), and
>> then populate that class with various data to form your simulation
>> model.  Let's say your simulation is called sim.app.Foo.  The most
>> straightforward way to run your simulation is to then call the
>> following method:
>>
>>     doLoop(sim.app.Foo.class, ...)
>>
>> In Scheme, I create the subclass using define-simple-class:
>>
>>     (define-simple-class <Foo> (<sim.engine.SimState>) ... )
>>
>> But I am utterly stymied as to how to pass the "Class" for the <Foo>
>> scheme class into the doLoop method.  For example, this works using a
>> preexisting Java simulation:
>>
>> (<sim.engine.SimState>:doLoop <sim.app.tutorial1and2.Tutorial1>:class
>> (<java.lang.String[]> (*:toString "-help")))
>>
>> But doing something like this:
>>
>> (<sim.engine.SimState>:doLoop <Foo>:class (<java.lang.String[]>
>> (*:toString "-help")))
>>
>> ... fails because <Foo> isn't a class but is a gnu.bytecode.ClassType,
>> and the documentation is pretty scarce about how (or if it's even
>> possible) to extract the class proper out of this beast.  Ideas?
>>
>> Sean
> 
> 


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