Java constants in Erjang?

28 views
Skip to first unread message

dun...@cogitat.io

unread,
Apr 10, 2014, 3:11:51 PM4/10/14
to erj...@googlegroups.com
Hey all,

When accessing Java constants from Clojure, you do this:

user=> java.lang.Math/PI
3.141592653589793


Note the absence of calling parens. Here's how you make a call to the sin method:

user=> (java.lang.Math/sin 0.5)
0.479425538604203


Calling Java functions from Erjang is clear

1> 'java.lang.Math':sin(0.5).
0.479425538604203


And, from LFE:

> (java.lang.Math:sin 0.5)
0.479425538604203


But is there any way to access constant field values?

Thanks!

d

Kresten Krab Thorup

unread,
Apr 10, 2014, 3:52:22 PM4/10/14
to erj...@googlegroups.com
Actually, it’s pretty close … you need an empty ‘java’ module, so you can activate the BIF’s java:get_static/2 and java:call/4.

krab$ cat > java.erl
-module(java).
^D
krab$ ./jerl
** Erjang R15B03 **  [root:/usr/local/lib/erlang] [erts:5.9.3.1] [smp S:2 A:10] [java:1.7.0_51] [unicode]
Eshell V5.9.3.1  (abort with ^G)
1> c(java).
{ok,java}
2> java:get_static('java.lang.Math', 'PI').
3.141592653589793
3> java:call(234, 'getClass', [], []).
'erjang.ESmall'

 
java:call’s 3rd argument is a list of atoms, designating the declared types of arguments.

The BIFs are defined in src/main/java/erjang/m/java/Native.java if you want to see their implementations.

Kresten


Mobile: + 45 2343 4626 | Skype: krabtrifork | Twitter: @drkrab
Trifork A/S  |  Margrethepladsen 4  | DK- 8000 Aarhus C |  Phone : +45 8732 8787  |  www.trifork.com

-- 
You received this message because you are subscribed to the Google Groups "Erjang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erjang+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

dun...@cogitat.io

unread,
Apr 10, 2014, 5:32:51 PM4/10/14
to erj...@googlegroups.com
Thanks so much, Kresten!

With that, I was able to update this page with more details on using Java from LFE:
  http://lfe.github.io/user-guide/java/erjang/2.html

Additionally, it gave me what I needed to poke around with the File object question I had earlier. Response to that forth-coming...

d
Reply all
Reply to author
Forward
0 new messages