On 3/08/2014 10:57 p.m., Bayan Rafeh wrote:
Wow clearly I misunderstood a lot of stuff.
On Sunday, 3 August 2014 at 08:07:04 UTC, Rikki Cattermole wrote:
On 3/08/2014 7:36 p.m., Bayan Rafeh wrote:
A small background on this:
I'm a university student about to start my graduation project with two
teammates, both of which have a C/Java/Python background and I suggested
we use D for our project. They're not familiar with it, so I wrote a
short tutorial for them here: https://github.com/bayanr/d-tutorial.
I wanted to see what you guys thought, and if this could be a useful
introduction for people with similar backgrounds. It's a first draft so
it probably has a lot of mistakes, omissions, etc. so any criticism
is welcome.
Your introduction to templates is far too simplistic.
What you introduced was template blocks. But showing them as templated
classes.
There is also templated functions, methods ext. After all.
It may also be wise to introduce CTFE as well. But not so important.
That's definitely going in there but I'm just making sure I fix
everything I have so far. If the responses so far are any indication
there is a lot of fixing that needs to be done.
I.e.
class ArrayList(T) {
T[] l;
}
alias IntList = ArrayList!int;
IntList list = new IntList();
would probably be better.
Could you give any suggestions for some example use cases that can
properly demo templates? If not I'll think of some.
Templates are a very complex beast to give examples for. But here are
some simple ones:
1.
class Something : ASomething {
void dosomething(T : ASomething2)(T value) {
import std.stdio;
writeln(value.getter);
}
}
2.
class List(T : Object) {
T[] items;
}
3.
std.regex.ctRegex (I'll let you grab that).
4.
Dump from a side project I'm working on.
module dwc.interfaces.eventable;
import std.string : toUpper;
import std.algorithm : filter, moveAll;
/**
* Provides an interface version of an eventing mechanism.
*
* See_Also:
* Eventing
*/
mixin template IEventing(string name, T...) {
mixin("void add" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(bool
delegate(T));});
mixin("void add" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(void
delegate(T));});
mixin("void remove" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(bool
delegate(T));});
mixin("void remove" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(void
delegate(T));});
mixin("void " ~ name ~ q{(T args);});
mixin("void clear" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(T
args);});
static if (T.length > 0) {
static if (__traits(compiles, typeof(this)) && is(typeof(this)
: T[0])) {
mixin("void " ~ name ~ q{(T[1 .. $] args);});
}
}
}
/**
* Implements an eventable interface for something.
* Includes support for bool delegate(T) and void delegate(T).
* Will consume a call to all delegates if it returns true. Default false.
*
* Example usage:
* mixin Eventing!("onNewListing", ListableObject);
*
* If is(T[0] == typeof(this)) then it'll use this as being the first
argument.
*/
mixin template Eventing(string name, T...) {
mixin(q{bool delegate(T)[] } ~ name ~ "_;");
mixin(q{bool delegate(T)[void delegate(T)] } ~ name ~ "_assoc;");
mixin("void add" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(void
delegate(T) value) {
mixin(name ~ "_ ~= (T args) => {value(args); return
false;}();");
mixin(name ~ "_assoc[value] = " ~ name ~ "_[$-1];");
}});
mixin("void add" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(bool
delegate(T) value) {
mixin(name ~ "_ ~= value;");
}});
mixin("void remove" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(bool
delegate(T) value) {
mixin("moveAll(filter!(a => a !is value)(" ~ name ~ "_), " ~ name ~
"_);");
}});
mixin("void remove" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(void
delegate(T) value) {
mixin("moveAll(filter!(a => a !is " ~ name ~ "_assoc.get(value,
null))(" ~ name ~ "_), " ~ name ~ "_);");
}});
mixin("void " ~ name ~ q{(T args) {
foreach (del; mixin(name ~ "_")) {
del(args);
}
}});
mixin("void clear" ~ toUpper(name[0] ~ "") ~ name[1 ..$] ~ q{(T args) {
mixin(name ~ "_") = [];
}});
static if (__traits(compiles, typeof(this)) && is(typeof(this) : T[0]))
{
mixin("void " ~ name ~ q{(T[1 .. $] args) {
foreach (del; mixin(name ~ "_")) {
if (del(this, args))
return;
}
}});
} else {
mixin("void " ~ name ~ q{(T args) {
foreach (del; mixin(name ~ "_")) {
if (del(args))
return;
}
}});
}
}
Everything else looks good including mixin templates. But just for
food for thought. With statements are cool.
I'll be sure to add those as well. I just remembered I forgot to add
scope too.
With statements are just so neat in my opinion. Just think of the
possibilities for GWT like libraries.
WebPage page;
with(page ~= new Layout) {
with(~= new Button) {
label = "Some text";
}
type = Layouts.Sequential;
//...
}
Hope to hear more of how the project goes.
We're presenting at the end of the next spring semester, I'll be sure to
share it here once it's complete.
-- Functions --
Ref implies the type is a reference type, i.e. changes inside the
functions will change the variable outside the function. in means
immutable out is an alias for ref(preferably used for parameters)
`ref` doesn't "imply" a reference, it "declares" or "states" or
something.
I'm basing this on the wiki: http://dlang.org/function.html
"ref parameter is passed by reference"
If that is not the case, in practical terms what does that mean exactly?
ref is like how in c++ has &var for function arguments. Basically passes
a value by pointer while not requiring you on the caller end to pass by
pointer and at the callee end use it as a pointer.
`in` doesn't mean immutable, it means scope const.
`out` is similar to `ref`, but it's not an alias. Unlike `ref`,
an out "parameter is initialized upon function entry with the
default value for its type".
Sorry I'll fix those shortly.
-- Operator Overloading --
The wording suggests that the list of operators is supposed to be
exhaustive. It's not.
I intended it to be comprehensive(with regard to types at least). What
am I missing regarding the different types of operators besides the
opAssigns?
unitary
unary
Ah crap, ok will fix that as well.
-- Templates --
Templates are D's response to Java generics and C++ templates.
At least put C++ first if you have to include Java ;)
Riots would ensue. To put it lightly we're not fans of C++. Well we're
not exactly fans of Java either :p
Basically templates are metaclasses that take types as parameters.
Template parameters are not restricted to types. They can also be
values or symbols.
I guess I need a more exhaustive list of examples.
alias List!int.ArrayList IntList;
Maybe use the more fashionable assignment syntax:
alias IntList = List!int.ArrayList;
-- Arrays --
a[x..y] is an array containing a[x] all the way to a[y]
To avoid confusion: all the way up to, but not including, a[y].
Will fix that as well.
PS. I applied some of the changes, though I left the stuff that I need
clarification on.
Thank you very much for the comments so far, please keep them coming.