questions on class definitions and mixins
Jakob Ovrum
jakobovrum at gmail.com
Mon Feb 10 07:57:34 PST 2014
On Monday, 10 February 2014 at 15:35:00 UTC, Patrick D. Jeeves
wrote:
> One thing I've been trying to figure out is how to do the
> following in D:
>
> class foo
> {
> string bar();
> };
>
> void foo::bar()
> {
> return "hello world!";
> }
>
> From what I can gather constructs like this just aren't allowed
> in D, but I don't understand why, can anyone explain it please?
> I know they're only in C++ because it uses #include instead of
> importing compiled modules, but I find it hard to quickly get a
> general idea of what a class is supposed to do when there are
> function definitions strewn about it.
There have been lengthy discussions about this before, perhaps
try searching the forums for them (I can't quite remember the
title of the most recent one...).
Personally I recommend generating documentation (using DDoc et
al) for your types and referencing at that.
> The other thing I want to know is about the mixin() command,
> and what the limitations of it are; I know it runs an
> interpreted version of D, but I get the feeling that it isn't
> as powerful as the compiled version, because no one seems to
> have tried making something like this:
>
> class example : File
> {
> mixin(d.flex(`lex_file.l`));
> mixin(d.bison(`bison_file.y`));
> };
>
> void main()
> {
> example("some_file.txt");
> example.yyparse();
> };
>
> Is this because it isn't possible to do such things, or because
> it would be rather pointless to do so compared to simpler
> approaches, (in this case calling the C function output by
> flex/bison).
There are D projects that do this kind of thing, e.g. Pegged[1]
and a proposed std.lexer[2].
Mixin expressions can mixin any string available/computable at
compile-time and there are no limits to what the mixed-in code
can do. However, if the mixed-in code is subsequently executed
*at compile-time*, it is limited by the same restrictions all
compile-time executation have[3].
[1] https://github.com/PhilippeSigaud/Pegged
[2]
https://github.com/Hackerpilot/Dscanner/blob/master/stdx/lexer.d
[3] http://dlang.org/function#interpretation
More information about the Digitalmars-d-learn
mailing list