questions on class definitions and mixins

Stanislav Blinov stanislav.blinov at gmail.com
Mon Feb 10 07:51:17 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.

dmd's -H family of options allows you to generate .di files which 
strip definitions, leaving only declarations in place.
I agree that sometimes finding your way around a huge class or 
module is difficult. However, D is not the only language that 
does this (C#, Java, Python...), and it's still manageable using 
DDoc and .di files.

> 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,

It doesn't run any interpreters, it mixes in a string as D code 
and compiles it.

> 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`));
> };

There's a string import expression 
(http://dlang.org/expression.html#ImportExpression) that allows 
to import some text file at compile time and make a string 
literal out of it. vibe.d, for example, uses it to generate code 
for its 'Diet' HTML templates.
Generally, if you can create compile-time parser for something, 
you can implement what you're describing above.


More information about the Digitalmars-d-learn mailing list