Debug help - foreach opApply overload School example: Programming in D

Brother Bill brotherbill at mail.com
Mon Aug 18 13:32:57 UTC 2025


Page 496 of Programming in D

Going in circles.  Please correct so it compiles and works.
An explanation would be helpful also.

Error Message
```
c:\dev\D\71 - 80\c73_2d_School_Student_Teacher\source\app.d(23): 
Error: cannot implicitly convert expression 
`this.students[cast(ulong)i]` of type `const(Student)` to 
`app.Student`
             Student student = students[i];
                                       ^
```

source/app.d
```
import std.stdio;

void main() {
	auto school = new School;

	foreach (Student student; school) {

	}

	foreach (Teacher teacher; school) {

	}
}

class School {
	Student[] students;
	Teacher[] teachers;

	int opApply(int delegate(ref Student) dg) const {
		int result = 0;

		for (int i = 0; i < students.length; ++i) {
			Student student = students[i];
			result = dg(student);
			if (result) break;
		}

		return result;
	}

	int opApply(int delegate(ref Teacher) dg) const {
		int result = 0;

		return result;
	}
}

class Student {
	string name;
}

class Teacher {
	string name;
}
```


More information about the Digitalmars-d-learn mailing list