questions on class definitions and mixins
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. 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).
Re: questions on class definitions and mixins
On Monday, 10 February 2014 at 15:41:06 UTC, Adam D. Ruppe wrote: On Monday, 10 February 2014 at 15:35:00 UTC, Patrick D. Jeeves wrote: [...] 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). I think it is just that nobody has tried yet. There are parser generators that work at compile time in D though https://github.com/PhilippeSigaud/Pegged for example I think it's not the only reason. I've tried to do something similar but not all libraries can be used at compile time. E.g: let's try to read a xml file at compile time...
Re: questions on class definitions and mixins
Adam D. Ruppe: It just wasn't there but there's some people who think it would be a good idea to add (including Walter I think) so it might be in later. It increases the syntactic variance of D code, and people coming from C++ will go using it straight (and not just in the uncommon cases discussed by Manu). Perhaps it could be added with an ugly syntax that discourages it usage in 99% of the cases, like: void __external_class_method ClassName.methodName() {} Bye, bearophile
Re: questions on class definitions and mixins
On Monday, 10 February 2014 at 15:35:00 UTC, Patrick D. Jeeves wrote: 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? http://wiki.dlang.org/DIP47 There is a threadnaught related to the above DIP somewhere. I personally disagreed with the suggestion.
Re: questions on class definitions and mixins
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