Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 11, 2016
0 parents commit c745674
Show file tree
Hide file tree
Showing 117 changed files with 1,442 additions and 0 deletions.
Binary file added 9781590599600.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Chapter01/greetings.d
@@ -0,0 +1,9 @@
module greetings;

import tango.io.Stdout;

void hello (char[][] names)
{
foreach (name; names)
Stdout.formatln ("hello {}", name);
}
6 changes: 6 additions & 0 deletions Chapter01/hello1.d
@@ -0,0 +1,6 @@
import tango.io.Stdout;

void main()
{
Stdout ("hello world").newline;
}
10 changes: 10 additions & 0 deletions Chapter01/hello2.d
@@ -0,0 +1,10 @@
import tango.io.Stdout;

void main (char[][] args)
{
if (args.length < 2)
Stdout ("usage is: hello name [name] ... [name]").newline;
else
foreach (name; args[1..$])
Stdout.formatln ("hello {}", name);
}
6 changes: 6 additions & 0 deletions Chapter01/hello3.d
@@ -0,0 +1,6 @@
import greetings;

void main(char[][] args)
{
hello (args[1..$]);
}
26 changes: 26 additions & 0 deletions Chapter02/parameters.d
@@ -0,0 +1,26 @@
import tango.io.Stdout;

void main()
{
int x = 1;
int y = 2;
int z = 3;

Stdout.formatln("In main, x: {} y: {} z: {}", x, y, z);

// Call some function.
someFunction(x, y, z);

Stdout.formatln("In main again, x: {} y: {} z: {}", x, y, z);
}

void someFunction(int x, out int y, ref int z)
{
Stdout.formatln("In someFunction, x: {} y: {} z: {}", x, y, z);

x = 10;
y = 20;
z = 30;

Stdout.formatln("In someFunction again, x: {} y: {} z: {}", x, y, z);
}
17 changes: 17 additions & 0 deletions Chapter02/properties.d
@@ -0,0 +1,17 @@
import tango.io.Stdout;

void main()
{
Stdout.formatln("int.init is {}", int.init);
Stdout.formatln("int.sizeof is {}", int.sizeof);
Stdout.formatln("int.alignof is {}", int.alignof);
Stdout.formatln("int.mangleof is '{}'", int.mangleof);
Stdout.formatln("int.stringof is '{}'", int.stringof);

int x;
Stdout.formatln("x.init is {}", x.init);
Stdout.formatln("x.sizeof is {}", x.sizeof);
Stdout.formatln("x.alignof is {}", x.alignof);
Stdout.formatln("x.mangleof is '{}'", x.mangleof);
Stdout.formatln("x.stringof is '{}'", x.stringof);
}
34 changes: 34 additions & 0 deletions Chapter02/scope.d
@@ -0,0 +1,34 @@
// This is module scope. Here, we declare x and initialize it with a constant
// expression.
int x = 1;

void main()
{ // A new block scope starts here--a child of the module scope.

// y is declared inside main's block scope, meaning it is local to main.
// It can see x, but x can't see it.
int y = x;

if(1 < 2)
{ // A new block scope starts here--a child of main's scope.

// Because x is visible in main's scope, it is also visible here. And
// because main's scope is this scope's parent, y is visible, too.
// However, z is visible neither in main's scope nor in the module
// scope.
int z = x + y;

} // The end of the if block scope

} // The end of main's block scope

void someFunc()
{ // A new block scope starts here--a child of the module scope and a
// sibling of main's scope.

// This y is declared inside someFunc's scope. It can see x, but x can't
// see it. Also, neither it nor the y in main's scope are visible to each
// other.
int y = x;

} // The end of someFunc's block scope
1 change: 1 addition & 0 deletions Chapter03/example1.d
@@ -0,0 +1 @@
module Time;
8 changes: 8 additions & 0 deletions Chapter03/example10.d
@@ -0,0 +1,8 @@
module Parser;

int state;

class Parser {
int state;
int outerState() { return .state; }
}
5 changes: 5 additions & 0 deletions Chapter03/example11.d
@@ -0,0 +1,5 @@
uint square (int x) { return x*x; }

unittest {
assert (square(4) == 16);
}
10 changes: 10 additions & 0 deletions Chapter03/example12.d
@@ -0,0 +1,10 @@
struct Time {
uint hour;
int timeZone;
bool usingAM;

int time() {
if (usingAM && hour > 12) return hour - 12;
else return hour;
}
}
4 changes: 4 additions & 0 deletions Chapter03/example13.d
@@ -0,0 +1,4 @@
struct S {
align(4) int a;
align(4) int b;
}
4 changes: 4 additions & 0 deletions Chapter03/example14.d
@@ -0,0 +1,4 @@
align(1) struct S {
int a;
int b;
}
4 changes: 4 additions & 0 deletions Chapter03/example15.d
@@ -0,0 +1,4 @@
union Error {
int errorCode;
char[] errorMessage;
}
5 changes: 5 additions & 0 deletions Chapter03/example16.d
@@ -0,0 +1,5 @@
import example12;

static Time t1 = { hour:7, timeZone:-2 };

static Time t2 = { 7, -2 };
7 changes: 7 additions & 0 deletions Chapter03/example17.d
@@ -0,0 +1,7 @@
import example12;

void main() {
Time t;
t.hour = 3;
Time at = t;
}
11 changes: 11 additions & 0 deletions Chapter03/example18.d
@@ -0,0 +1,11 @@
struct Time {
int hour;

static Time opAssign(int time) {
Time t;
t.hour = time;
return t;
}
}

Time t = Time(3);
2 changes: 2 additions & 0 deletions Chapter03/example19.d
@@ -0,0 +1,2 @@
void setTime(Time t);
setTime( Time(1, 2) );
1 change: 1 addition & 0 deletions Chapter03/example2.d
@@ -0,0 +1 @@
module tango.time.Time;
5 changes: 5 additions & 0 deletions Chapter03/example20.d
@@ -0,0 +1,5 @@
import tango.time.Time;

void main() {
Time * dt = new Time;
}
11 changes: 11 additions & 0 deletions Chapter03/example21.d
@@ -0,0 +1,11 @@
class Time {
uint h;
int tz;

uint hour() { return h; }

this(uint hour, int timeZone) {
this.h = hour;
this.tz = timeZone;
}
}
5 changes: 5 additions & 0 deletions Chapter03/example22.d
@@ -0,0 +1,5 @@
import tango.io.File;

void main() {
File f = new File("tango/io/File.d");
}
5 changes: 5 additions & 0 deletions Chapter03/example23.d
@@ -0,0 +1,5 @@
import tango.io.File;

void main() {
scope f = new File("tango/io/File.d");
}
6 changes: 6 additions & 0 deletions Chapter03/example24.d
@@ -0,0 +1,6 @@
class Time {
private uint month; // 0 is January, 11 December
invariant() {
assert ( month < 12 );
}
}
4 changes: 4 additions & 0 deletions Chapter03/example25.d
@@ -0,0 +1,4 @@
void main() {
auto o = new Object;
assert (o);
}
8 changes: 8 additions & 0 deletions Chapter03/example26.d
@@ -0,0 +1,8 @@
import tango.io.FilePath;

class FileConduit : DeviceConduit {
private PathView path_;
PathView path() { return path_; }
}

class DeviceConduit { }
1 change: 1 addition & 0 deletions Chapter03/example27.d
@@ -0,0 +1 @@
final class FilePath { }
5 changes: 5 additions & 0 deletions Chapter03/example28.d
@@ -0,0 +1,5 @@
abstract class Conduit {

abstract uint write (void [] src);

}
9 changes: 9 additions & 0 deletions Chapter03/example29.d
@@ -0,0 +1,9 @@
import tango.io.model.IConduit;

class MyProducer {
void[] buffer;
void produce(InputStream data) {
data.read(buffer);
data.clear.close;
}
}
1 change: 1 addition & 0 deletions Chapter03/example3.d
@@ -0,0 +1 @@
import tango.time.Time;
11 changes: 11 additions & 0 deletions Chapter03/example30.d
@@ -0,0 +1,11 @@
class Pipe : DeviceConduit {
alias DeviceConduit.copy copy;
alias DeviceConduit.read read;
}

class DeviceConduit {

void copy () {}
void read () {}

}
14 changes: 14 additions & 0 deletions Chapter03/example31.d
@@ -0,0 +1,14 @@
class FileConduit : DeviceConduit {
private void closeFile() { }

override void close() {
super.close();
closeFile();
}
}

class DeviceConduit {

void close() { }

}
7 changes: 7 additions & 0 deletions Chapter03/example32.d
@@ -0,0 +1,7 @@
class DataInput : InputStream
{
public DataInput clear()
{
super.clear;
}
}
20 changes: 20 additions & 0 deletions Chapter03/example33.d
@@ -0,0 +1,20 @@
import tango.io.Conduit;

class SocketConduit : Conduit {
private static SocketConduit freelist;
private SocketConduit next;

package static synchronized SocketConduit allocate ()
{
SocketConduit s;

if (freelist)
{
s = freelist;
freelist = s.next;
}
else
{ }
return s;
}
}
1 change: 1 addition & 0 deletions Chapter03/example34.d
@@ -0,0 +1 @@
SocketConduit sc = SocketConduit.allocate();
9 changes: 9 additions & 0 deletions Chapter03/example35.d
@@ -0,0 +1,9 @@
class SC : C {
private int i;
this (int i) { this(i, true); }
this (int i, bool b) { super (i, b); }
}

class C {
this (int i, bool b) { }
}
8 changes: 8 additions & 0 deletions Chapter03/example36.d
@@ -0,0 +1,8 @@
class Outer {
class Nested { }
}

void main() {
Outer o = new Outer;
Outer.Nested nested = o.new Nested;
}
18 changes: 18 additions & 0 deletions Chapter03/example37.d
@@ -0,0 +1,18 @@
interface EventHandler { void handle(Event); }

class Event {
void printDetails();
}

class Events {
void registerHandler(EventHandler eh) { }
}

void main() {
auto e = new Events;
e.registerHandler(
new class EventHandler {
void handle(Event e) { e.printDetails(); }
}
);
}
7 changes: 7 additions & 0 deletions Chapter03/example38.d
@@ -0,0 +1,7 @@
import tango.io.FilePath;

class File {
private PathView path_;
void path(PathView p) { path_ = p; }
PathView path() { return path_; }
}
15 changes: 15 additions & 0 deletions Chapter03/example39.d
@@ -0,0 +1,15 @@
import tango.io.Stdout;
import tango.io.FilePath;

class File {
private FilePath path_;
void path(FilePath newpath) { path_ = newpath; }
FilePath path() { return path_; }
}

void main() {
File f = new File;
f.path = new FilePath("tango/io/FilePath.d");

Stdout (f.path);
}
1 change: 1 addition & 0 deletions Chapter03/example4.d
@@ -0,0 +1 @@
public import tango.time.Time;
7 changes: 7 additions & 0 deletions Chapter03/example40.d
@@ -0,0 +1,7 @@
interface IConduit { }

interface InputStream {
IConduit conduit ();
uint read (void[] dst);
void clear ();
}

0 comments on commit c745674

Please sign in to comment.