Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 22:50:27 UTC, Stanislav Blinov wrote: On Tuesday, 28 January 2014 at 22:35:31 UTC, Martin Cejp wrote: I really wonder whether the rule could be relaxed a little bit. o_O How? Not being a keyword except in places where it is used as such. Only if it's not a keyword at all, lest you complicate parsing. You wouldn't want any other keyword (I dunno, int?) get a special treatment now, would you? Don't get me wrong, I'm all for allowing to use body as an identifier in user code, I just don't see any half-measures in that regard :) It's surprisingly easy to implement, see here: https://github.com/schuetzm/dmd/commit/955be4ef9f26e92f7a0d5dedf86b64a03d903be8 It requires only a few trivial changes to the compiler, but I have to admit that it makes the code uglier. In particular, it doesn't scale; every keyword treated in this way requires additional special handling. I'm not sure whether I should make a PR from this...
Re: Keywords: How to trick the compiler?
On Wednesday, 29 January 2014 at 17:12:57 UTC, Stanislav Blinov wrote: On Wednesday, 29 January 2014 at 17:11:32 UTC, Ary Borenszweig wrote: Yes, as there are other ways for body: _body, _body, Body, HtmlBody, etc. But body is the best one. torso? ;) offspring = document.createElement(div); document.torso.addOffspring(div);
Re: Keywords: How to trick the compiler?
On Thursday, 30 January 2014 at 10:15:10 UTC, Chris wrote: offspring = document.createElement(div); document.torso.addOffspring(div); Looks great! :D
Re: Keywords: How to trick the compiler?
On Wednesday, 29 January 2014 at 17:11:32 UTC, Ary Borenszweig wrote: Yes, as there are other ways for body: _body, _body, Body, HtmlBody, etc. But body is the best one. torso? ;)
Re: Keywords: How to trick the compiler?
On 1/28/14, 3:17 PM, Chris wrote: On Tuesday, 28 January 2014 at 17:18:44 UTC, Ary Borenszweig wrote: On 1/28/14, 11:30 AM, bearophile wrote: Ary Borenszweig: In Ruby you can do this: class Foo def end 1 end end Foo.new.end Ruby is a different language from D, they define code safety in quite different ways. Bye, bearophile Having a method named end doesn't make the language less safe. End is cumbersome and looks awkward. Sure there are other ways ;) Yes, as there are other ways for body: _body, _body, Body, HtmlBody, etc. But body is the best one.
Re: Keywords: How to trick the compiler?
On 1/28/14, Chris wend...@tcd.ie wrote: Is there a simple way to trick the compiler (e.g. with alias), if a keyword conflicts with a function/method, e.g. as in a HTML document: auto docBody = document.body; body is probably the most frequent issue I run into when porting C/C++ code to D. I really wonder whether the rule could be relaxed a little bit.
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 12:57:02 UTC, Andrej Mitrovic wrote: body is probably the most frequent issue I run into when porting C/C++ code to D. I really wonder whether the rule could be relaxed a little bit. o_O How?
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 12:58:29 UTC, Stanislav Blinov wrote: I really wonder whether the rule could be relaxed a little bit. o_O How? If body never appears after a ., and is only before a { then there is no conflict?
Re: Keywords: How to trick the compiler?
On 1/28/14, 10:00 AM, Ola Fosheim Grøstad ola.fosheim.grostad+dl...@gmail.com wrote: On Tuesday, 28 January 2014 at 12:58:29 UTC, Stanislav Blinov wrote: I really wonder whether the rule could be relaxed a little bit. o_O How? If body never appears after a ., and is only before a { then there is no conflict? Exactly: body is only a conflict when appearing alone. But after a . it's perfectly fine. In Ruby you can do this: class Foo def end 1 end end Foo.new.end
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 11:22:10 UTC, Chris wrote: Is there a simple way to trick the compiler (e.g. with alias), if a keyword conflicts with a function/method, e.g. as in a HTML document: alas, no. In my dom.d, I called that method mainBody instead... If you are accessing a C function called body, you can access it using pragma mangle: pragma(mangle, body) void cBody() { } Still can't call it body in D, so have to add something, but the pragma mangle will make the name match up for the linker so it refers to the same C function.
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 14:30:36 UTC, bearophile wrote: Ruby is a different language from D, they define code safety in quite different ways. In D I think body actually is redundant since in/out always should be followed by a function body? Maybe it has more to do with parsing strategies. If you use a separate and fully independent lexer then the symbol body might be lost and replaced by a keyword enumeration so by tradition you reserve all keywords?
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 12:57:02 UTC, Andrej Mitrovic wrote: body is probably the most frequent issue I run into when porting C/C++ code to D. I really wonder whether the rule could be relaxed a little bit. All my input and output streams in Java are called in and out. x.x Reminds me of max and min in competitive programming in C++.
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 16:14:48 UTC, Casper Færgemand wrote: On Tuesday, 28 January 2014 at 12:57:02 UTC, Andrej Mitrovic wrote: body is probably the most frequent issue I run into when porting C/C++ code to D. I really wonder whether the rule could be relaxed a little bit. All my input and output streams in Java are called in and out. x.x Reminds me of max and min in competitive programming in C++. Some kind of aliasing mechanism would be nice to escape keyword restrictions. It's a bit awkward to have document.body_ or document.Body or whatnot. But I guess it's not trivial to implement that without breaking code safety and parsing mechanisms.
Re: Keywords: How to trick the compiler?
On 1/28/14, 11:30 AM, bearophile wrote: Ary Borenszweig: In Ruby you can do this: class Foo def end 1 end end Foo.new.end Ruby is a different language from D, they define code safety in quite different ways. Bye, bearophile Having a method named end doesn't make the language less safe.
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 17:18:44 UTC, Ary Borenszweig wrote: On 1/28/14, 11:30 AM, bearophile wrote: Ary Borenszweig: In Ruby you can do this: class Foo def end 1 end end Foo.new.end Ruby is a different language from D, they define code safety in quite different ways. Bye, bearophile Having a method named end doesn't make the language less safe. End is cumbersome and looks awkward. Sure there are other ways ;)
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 12:58:29 UTC, Stanislav Blinov wrote: On Tuesday, 28 January 2014 at 12:57:02 UTC, Andrej Mitrovic wrote: body is probably the most frequent issue I run into when porting C/C++ code to D. I really wonder whether the rule could be relaxed a little bit. o_O How? Not being a keyword except in places where it is used as such.
Re: Keywords: How to trick the compiler?
On Tuesday, 28 January 2014 at 22:35:31 UTC, Martin Cejp wrote: I really wonder whether the rule could be relaxed a little bit. o_O How? Not being a keyword except in places where it is used as such. Only if it's not a keyword at all, lest you complicate parsing. You wouldn't want any other keyword (I dunno, int?) get a special treatment now, would you? Don't get me wrong, I'm all for allowing to use body as an identifier in user code, I just don't see any half-measures in that regard :)