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]

multi-methods?


How it is possible to emulate a multi-methods?

example:

(define-simple-class Figure ())
(define-simple-class Circle (Figure))
(define-simple-class Ball (Figure))

(define-simple-class Something ()

  ((doSomething (circle :: Circle)) :: void allocation: 'static
    (display "Circle!\n"))

  ((doSomething (ball :: Ball)) :: void allocation: 'static
    (display "Ball!\n"))

  ((doSomething (fig :: Figure)) allocation: 'static
    (display "A figure!\n")))

;; test call
(Something:doSomething (Ball)) ;; output "Ball!"

;; JVM not have multi-methods
(define fig :: Figure (Ball))
(Something:doSomething fig) ;; output "A figure!"

(define fig2 (Ball))
(Something:doSomething fig2) ;; output "Ball!", compile warning


How get result as in last method call, but with typed variable?

Disassemble of code:
public final void run(gnu.mapping.CallContext);
  Code:
   0:   aload_1
   1:   getfield        #6; //Field
gnu/mapping/CallContext.consumer:Lgnu/lists/Consumer;
   4:   astore_2
   5:   new     #8; //class Ball
   8:   dup
   9:   invokespecial   #12; //Method Ball."<init>":()V
   12:  invokestatic    #18; //Method Something.doSomething:(LBall;)V
   15:  aload_0
   16:  new     #8; //class Ball
   19:  dup
   20:  invokespecial   #12; //Method Ball."<init>":()V
   23:  putfield        #24; //Field fig:LFigure;
   26:  aload_0
   27:  getfield        #24; //Field fig:LFigure;
   30:  invokestatic    #27; //Method
Something.doSomething:(LFigure;)Ljava/lang/Object;
   33:  aload_2
   34:  invokestatic    #33; //Method
gnu/mapping/Values.writeValues:(Ljava/lang/Object;Lgnu/lists/Consumer;)V
   37:  aload_0
   38:  getfield        #37; //Field fig2:Lgnu/mapping/Location;
   41:  new     #8; //class Ball
   44:  dup
   45:  invokespecial   #12; //Method Ball."<init>":()V
   48:  invokevirtual   #43; //Method
gnu/mapping/Location.set:(Ljava/lang/Object;)V
   51:  getstatic       #49; //Field
gnu/kawa/reflect/Invoke.invokeStatic:Lgnu/kawa/reflect/Invoke;
   54:  getstatic       #52; //Field Something:Ljava/lang/Class;
   57:  getstatic       #56; //Field Lit0:Lgnu/mapping/SimpleSymbol;
   60:  aload_0
   61:  getfield        #37; //Field fig2:Lgnu/mapping/Location;
   64:  invokevirtual   #60; //Method
gnu/mapping/Location.get:()Ljava/lang/Object;
   67:  aload_1
   68:  invokevirtual   #74; //Method
gnu/mapping/Procedure.check3:(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Lgnu/mapping/CallConte
xt;)V

Untyped variable is used with 'Location, but location of typed
variable is wrong:

#|kawa:3|# (define s :: String "sdfg")
#|kawa:4|# (location s)
Argument #1 (sdfg) to
'kawa.standard.location.makeLocationProc(gnu.mapping.Location)' has
wrong type (java.lang.String) (java.lang.String cannot
 be cast to gnu.mapping.Location)
        at atInteractiveLevel$2.run(stdin:4)
        at gnu.expr.ModuleExp.evalModule(ModuleExp.java:302)
        at kawa.Shell.run(Shell.java:275)
        at kawa.Shell.run(Shell.java:186)
        at kawa.Shell.run(Shell.java:167)
        at kawa.repl.main(repl.java:870)
Caused by: java.lang.ClassCastException: java.lang.String cannot be
cast to gnu.mapping.Location
        ... 6 more
#|kawa:5|#


How get needed result?

-- 
WBR, Yaroslav Kavenchuk.


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