calling yeti functions from Java

26 views
Skip to first unread message

Răzvan Rotaru

unread,
Jan 6, 2015, 5:07:21 PM1/6/15
to yeti...@googlegroups.com
Hi,

There is a small example in the source code which shows how to call a yeti function from java. I would like to do this from inside a class defined in yeti. Example:


myfun {a, b}
= a + b;

class MyClass(Object myYetiFun)
   
void someFunctionOfMyClass()
       
System#out#println("Now calling the yeti function");

        param = {a=10, b=20};

       
// How to call myYetiFun with param and get the result (and use it further in Java calls)?

        System#out#println(theResult);

        // And what is the result of the function is a structure?
        // Something like: System#out#println(theResult.value);
end;

myInstance = new MyClass(myfun);
myInstance#someFunctionOfMyClass();


The first problem is that I want to pass the yeti function to the Java class (I haven't figured its class out, so it's declared as Object). The second (possible) problem would be to get the result and use it further (in Java calls).

Cheers,
Răzvan

chrisichris

unread,
Jan 6, 2015, 6:05:47 PM1/6/15
to yeti...@googlegroups.com
The easiest way to handle this case is to define the java-class inside a yeti function

myClassFactory myYetiFun =
  (class MyClass 
       void someFunctionOfMyClass()
            res = myYetiFun {a = 10, b = 20};
            Foo#javaMethod(res)
   end;
   new MyClass());

(myClassFactory myfun)#someFunctionOfMyClass();

Răzvan Rotaru

unread,
Jan 7, 2015, 4:28:38 AM1/7/15
to yeti...@googlegroups.com

miercuri, 7 ianuarie 2015, 01:05:47 UTC+2, chrisichris a scris:
The easiest way to handle this case is to define the java-class inside a yeti function

myClassFactory myYetiFun =
  (class MyClass 
       void someFunctionOfMyClass()
            res = myYetiFun {a = 10, b = 20};
            Foo#javaMethod(res)
   end;
   new MyClass());

(myClassFactory myfun)#someFunctionOfMyClass();



This wouldn't really work for my case. I need to have each instance of MyClass hold reference to a different yeti function, which is coming from outside (is not defined in my module, but received as value from outside).

What I am trying to solve with this is to write a Jetty handler, which delegates the work to a yeti function.

Răzvan

Chris Cannam

unread,
Jan 7, 2015, 4:41:22 AM1/7/15
to yeti...@googlegroups.com


On Wed, Jan 7, 2015, at 09:28 AM, Răzvan Rotaru wrote:
>
> This wouldn't really work for my case. I need to have each instance of
> MyClass hold reference to a different yeti function, which is coming from
> outside (is not defined in my module, but received as value from
> outside).

If I understand you both correctly (?), that is what the example code
does. For another example, in an app that defines routers for Spark Java
I have something like this (ok there's a bit more code in it, but)

import spark: Request, Response, Route, Spark;

route match callback =
(handler path callback =
(class Handler extends Route(path)
Object handle(Request req, Response resp)
routeParams = customHash \(req#params());
callback { routeParams },
end;
new Handler());
case match of
Get path:
h = handler path callback;
Spark#get(h);
esac);

Here "callback" is not defined in this module, but instead in the code
that uses it.

I still find it instinctively surprising that this is so simple to do.


Chris

Madis Janson

unread,
Jan 7, 2015, 10:21:38 AM1/7/15
to yeti...@googlegroups.com
The handler functions callback argument could actually be omitted - the
class would then see the outer route functions callback argument, which
happens to be here exactly the same value.

Răzvan Rotaru

unread,
Jan 13, 2015, 8:10:53 AM1/13/15
to yeti...@googlegroups.com, ma...@cyber.ee
Great, it worked. Nesting the class inside the function did the job.

Does this create a new class every time the function is called? Is this bad for the performance?

Răzvan

Christian Essl

unread,
Jan 13, 2015, 8:14:20 AM1/13/15
to yeti...@googlegroups.com

No just a new instance which is in almost all cases no performance problem

--
You received this message because you are subscribed to the Google Groups "yeti-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yeti-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages