Is there something like #include in C

Sorry to ask, but I am a beginner.
I have a very extensive programme and would like to divide it into a few files. Is there something like the #include construct of C?

Hi @Buschmensch44 and welcome to the community.

Yes, we have import which is very similar, here you can find a pretty good summary of how to use this feature. But, if after reading it you still have problems do not doubt posting again with more details.

Given that you are new to the language I would also recommend you to pick a book or a course to help you with all the concepts, the language itself is somewhat complex and if you mix-in functional programming concepts like immutability and higher-order functions it may be a little bit too hard at the beginning.

Anyways, feel free to also jump in into gitter which is a real time chat room about all the things Scala for this kind of “basic questions” (whatever that means), it probably would be easier to ask and get help there.

1 Like

We don’t have #include, but we don’t miss it, either. In Scala — and this is true in Java as well — separate source files can reference each other directly without any header files or other ceremony. You just compile them together, and it works. Compiling all the files in a project together is something your IDE or build tool will normally do for you, without any special setup. Or if you’re using command-line scalac, you just scalac *.scala.


I do miss the feature of just including a small source file, just like #include in C, especially in Scala scripts where an sbt-project or even a package is overkill.

I found the Ammonite REPL for Scala overcomes this problem by providing

import $file.file_name

Have a look at

Thank you very much for answering my question. But unfortunately repl does not solve my problem.

Thank you very much for answering my question. But this only works if the individual files contain complete class or object definitions. This is unfortunately not the case with me.

In scala you don’t need something like #include. The compiler and runtime let you access everything that’s on the classpath.

That said, in order to avoid running out of names, things are organized into namespaces such as packages. That means that fully-qualified names can get long. In order to make code readable, languages like Java and Scala provide import, which can make members of another namespace available by their short name directly.

If a single class or object is too long for one file, there’s a good chance there’s another, more manageable way to organize your code. That said, the simplest modification to achieve your goal is probably to move as much code as you like into a trait and then make the original class or object extend that trait.


Many thanks to all contributors for their suggestions. I will look into them.

Another common approach is to simply declare functions in an object, and import the contents of that. The best approach sort of depends on what you’re trying to achieve – any chance you can describe the problem you’re trying to solve?

1 Like

@jducoeur Thank you very much for your answer.
This is the way I will follow.