emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Scala problem in Babel
@ 2017-04-02 12:39 Jarmo Hurri
  2017-04-03  8:10 ` Jarmo Hurri
  0 siblings, 1 reply; 2+ messages in thread
From: Jarmo Hurri @ 2017-04-02 12:39 UTC (permalink / raw)
  To: emacs-orgmode


Greetings.

I am illustrating some basic syntactic differences between programming
languages to some people. In order to do this I embedded a small Scala
program into an Org file

  #+BEGIN_SRC scala :exports both
    object Fibonacci extends App
    {
      def fib (n : Int) : Int =
      {
        if (n == 1 || n == 2)
          n - 1
        else
          fib (n - 1) + fib (n - 2)
      }

      println (fib (7))
    }
  #+END_SRC

This isn't in any way a correctly efficient way to (recursively)
calculate numbers from the sequence, but this is only about syntactics
for beginners, so I need to keep it very simple.

Anyway, when I compile and run this code in Scala it works just fine:

------------------
[jarmo@localhost tmp]$ scalac test.scala
[jarmo@localhost tmp]$ scala Fibonacci
8
------------------

But C-c C-c in Org mode gives me a warning and no result:

  #+RESULTS:
  : /tmp/babel-6890GcO/scala-6890K2a:6: warning: Fibonacci$2 has a main method with parameter type Array[String], but Main.$anon$2.$anonfun$1.Fibonacci$2 will not be a runnable program.
  :   Reason: companion contains its own main method, which means no static forwarder can be generated.
  : 
  : object Fibonacci extends App
  :        ^
  : one warning found
  : ()

When I look at the temporary file I see that the code has been embedded
in a wrapper defined in ob-scala.el. I guess it's the wrapper that
messes up the execution of the code.

What is the wrapper for? How am I supposed to use it?

Jarmo

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

* Re: Scala problem in Babel
  2017-04-02 12:39 Scala problem in Babel Jarmo Hurri
@ 2017-04-03  8:10 ` Jarmo Hurri
  0 siblings, 0 replies; 2+ messages in thread
From: Jarmo Hurri @ 2017-04-03  8:10 UTC (permalink / raw)
  To: emacs-orgmode


Jarmo Hurri <jarmo.hurri@iki.fi> writes:

> When I look at the temporary file I see that the code has been
> embedded in a wrapper defined in ob-scala.el. I guess it's the wrapper
> that messes up the execution of the code.

> What is the wrapper for? How am I supposed to use it?

Ok, got it by taking a closer look at ob-scala.el. In Babel scala code
isn't compiled and executed, it is run in interactive mode (shell). The
following works as expected (just as an example; you can also do this
without the Fibonacci class).

  #+BEGIN_SRC scala :exports both :results output
    object Fibonacci
    {
      def fib (n : Int) : Int =
      {
        if (n == 1 || n == 2)
          n - 1
        else
          fib (n - 1) + fib (n - 2)
      }
    }
    println (Fibonacci.fib (7))
  #+END_SRC

  #+RESULTS:
  : 8

Jarmo

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

end of thread, other threads:[~2017-04-03  8:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-02 12:39 Scala problem in Babel Jarmo Hurri
2017-04-03  8:10 ` Jarmo Hurri

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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).