How to create a library, I mean an external file that holds functions just like std.stdio
Carlos
checoimg at gmail.com
Tue Nov 19 18:14:28 PST 2013
So I'm reading this tut from Ali :)
And we I got to this part :
A code example. http://pastebin.com/ESeL7dfH
But I want to declare this functions "print" outside of the file
and call the file to be loaded by the compiler. So I can use the
same solution in various program without having to copy paste it
to each one.
Thanks you for your attention.
Checoimg
-------------------------------------
Main file :
-------------------------------------
import std.stdio;
import print;
void main()
{
int[] numbers;
int count;
write("How many numbers are you going to enter? ");
readf(" %s", &count);
// Read the numbers
foreach (i; 0 .. count) {
int number;
write("Number ", i, "? ");
readf(" %s", &number);
numbers ~= number;
}
// Print the numbers
writeln("Before sorting:");
print(numbers);
numbers.sort;
// Print the numbers
writeln("After sorting:");
print(numbers);
}
-----------------------------------------------
Library file :
-----------------------------------------------
void print(int[] slice)
{
foreach (i, element; slice) {
writefln("%3s:%5s", i, element);
}
}
----------------------------------------------
More information about the Digitalmars-d-learn
mailing list