Re: Writing Program Without main Function

2018-12-08 Thread Samir via Digitalmars-d-learn
On Saturday, 8 December 2018 at 03:30:30 UTC, Jonathan M Davis 
wrote:
There's one main per program, not per module. Where main lives 
depends on how the program was written, but dub encourages 
putting it in app.d, because that's what it generates when you 
create a new dub project. imports then provide access to other 
modules so that the code doing the importing can use it. You 
can think of it like the module with main being the start of 
the import tree, though it's a bit more complicated than that 
in practice, since modules can be compiled separately, and main 
only has to be there when the program is finally linked. But in 
concept, you have main and the module its in importing other 
modules, which then import other modules, etc. until you get 
all of the code in the program.


- Jonathan M Davis


Thanks for all of the great feedback and explanations, Jonathan!  
Looks like I need to add dub to the list of things I still need 
to learn about.


Re: Writing Program Without main Function

2018-12-07 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, December 7, 2018 2:42:33 PM MST Samir via Digitalmars-d-learn 
wrote:
> Ok.  Upon further investigation, I think I see what is going on.
> Most of the repos I am skimming are for this year's Advent of
> Code.  They structure their repo with an `app.d` file which does
> contain a `main` function but this program is structured such
> that it imports the files I was looking at earlier (which do not
> have a `main` function).
>
> Looks like I have to get smarter on how modules, mixins and
> imports work.

There's one main per program, not per module. Where main lives depends on
how the program was written, but dub encourages putting it in app.d, because
that's what it generates when you create a new dub project. imports then
provide access to other modules so that the code doing the importing can use
it. You can think of it like the module with main being the start of the
import tree, though it's a bit more complicated than that in practice, since
modules can be compiled separately, and main only has to be there when the
program is finally linked. But in concept, you have main and the module its
in importing other modules, which then import other modules, etc. until you
get all of the code in the program.

- Jonathan M Davis





Re: Writing Program Without main Function

2018-12-07 Thread Samir via Digitalmars-d-learn
Ok.  Upon further investigation, I think I see what is going on.  
Most of the repos I am skimming are for this year's Advent of 
Code.  They structure their repo with an `app.d` file which does 
contain a `main` function but this program is structured such 
that it imports the files I was looking at earlier (which do not 
have a `main` function).


Looks like I have to get smarter on how modules, mixins and 
imports work.


Re: Writing Program Without main Function

2018-12-07 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, December 7, 2018 2:02:59 PM MST Samir via Digitalmars-d-learn 
wrote:
> Is it possible to write and execute a D program without a main
> function?
>
> Most of my programs will start with some `import` statements,
> followed by any functions and then ending with the `main`
> function (e.g. `void main() {`).
>
> As I am just a beginner to programming and still new to D, I have
> been browsing some repositories that people have posted here to
> learn more about the language and how more experienced
> programmers write their code.  In many of these examples, they
> start with the `module` statement followed by their `import`
> statements and then will have sections defining various classes,
> structs, unit tests and other bits but I never see a `main`
> function.
>
> How do you compile and execute these programs?

The short answer is that every program must have a main function.

The long answer:

main is the entry point into the program. You can't do much without it, and
the linker will require that it exists. static constructors do get run
before main, and static destructors get run after it, so it is possible to
run code before and after main, but they're only intended for more complex
initialization and destruction of module-level and static variables. Really,
when it comes down to it, main _is_ your program, and there must be a main
function one way or another. That's true of pretty much any programming
language. Some may present main in a different way, but ultimately, every
programming language has some form of main, because every program needs an
entry point.

dmd provides the -main flag which will insert an empty main, so you could
use that, but it's really only of any use for unit testing (since in D,
unittest blocks are run before main). For an actual program, you basically
don't have anything without main. Essentially what you get in D is

1. Run static constructors.
2. If -unittest was used, run the unittest blocks (though since that's only
   intended for testing, it really shouldn't be in a production program).
3. Run main.
4. Run static destructors.

And for simpler programs, you basically have

1. Run main.

None of your stray functions or classes are going to be used just because
you declared them. It's main that decides what's going to be run, even if
it's just to call another function. All of those other functions and classes
are just a way to organize your program, make pieces reusable, etc.
Ultimately, the main function is your program, so it really doesn't make
sense to try to not have one.

The closest thing that exists to a program with no main is a library, and
libraries are just collections of functions and user-defined types which are
packaged together so that programs can use them. They aren't programs on
their own, just pieces of reusable code. Ultimately, you need an actual
program with a main to use them.

When you see code snippets that don't use main, it's because they're usually
just that - code snippets. They're showing a piece of code, not an entire
program. It's just assumed that anyone who wants to actually run them is
going to deal with declaring main themselves, because everyone knows that
you need a main function. As such, including main is often just extraneous
information.

- Jonathan M Davis





Re: Writing Program Without main Function

2018-12-07 Thread rikki cattermole via Digitalmars-d-learn

There is always a main function.
It doesn't matter in which module its in or language.
It just has to exist.


Writing Program Without main Function

2018-12-07 Thread Samir via Digitalmars-d-learn
Is it possible to write and execute a D program without a main 
function?


Most of my programs will start with some `import` statements, 
followed by any functions and then ending with the `main` 
function (e.g. `void main() {`).


As I am just a beginner to programming and still new to D, I have 
been browsing some repositories that people have posted here to 
learn more about the language and how more experienced 
programmers write their code.  In many of these examples, they 
start with the `module` statement followed by their `import` 
statements and then will have sections defining various classes, 
structs, unit tests and other bits but I never see a `main` 
function.


How do you compile and execute these programs?

Thanks
Samir