www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Keywords: How to trick the compiler?

reply "Chris" <wendlec tcd.ie> writes:
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;

// corresponding method (will not compile due to body being a 
keyword)

 property auto body() {
   // simplified code
   return this.getElementByTagName("body");
}
Jan 28 2014
next sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 28 January 2014 at 11:22:10 UTC, Chris wrote:

Nope, just choose a different name.
Jan 28 2014
parent reply "Chris" <wendlec tcd.ie> writes:
On Tuesday, 28 January 2014 at 11:24:33 UTC, Stanislav Blinov 
wrote:
 On Tuesday, 28 January 2014 at 11:22:10 UTC, Chris wrote:

 Nope, just choose a different name.
Cripes! There goes my API ...
Jan 28 2014
parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 28 January 2014 at 11:38:16 UTC, Chris wrote:
 On Tuesday, 28 January 2014 at 11:24:33 UTC, Stanislav Blinov 
 wrote:
 On Tuesday, 28 January 2014 at 11:22:10 UTC, Chris wrote:

 Nope, just choose a different name.
Cripes! There goes my API ...
Aww, a bit of underscoring hasn't hurt anyone yet. :)
Jan 28 2014
prev sibling next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
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:

 auto docBody = document.body;

 // corresponding method (will not compile due to body being a 
 keyword)

  property auto body() {
   // simplified code
   return this.getElementByTagName("body");
 }
Write in with an uppercase. ;) Int, If, Template...
Jan 28 2014
parent reply "simendsjo" <simendsjo gmail.com> writes:
On Tuesday, 28 January 2014 at 12:25:19 UTC, Namespace wrote:
 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:

 auto docBody = document.body;

 // corresponding method (will not compile due to body being a 
 keyword)

  property auto body() {
  // simplified code
  return this.getElementByTagName("body");
 }
Write in with an uppercase. ;) Int, If, Template...
I've also seen some people append _: body_, int_ etc.
Jan 28 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Tuesday, 28 January 2014 at 12:43:59 UTC, simendsjo wrote:
 I've also seen some people append _: body_, int_ etc.
It is what current Phobos style guide suggests.
Jan 28 2014
parent "Chris" <wendlec tcd.ie> writes:
On Tuesday, 28 January 2014 at 12:45:37 UTC, Dicebot wrote:
 On Tuesday, 28 January 2014 at 12:43:59 UTC, simendsjo wrote:
 I've also seen some people append _: body_, int_ etc.
It is what current Phobos style guide suggests.
Yes, I've used it, but don't like it. Maybe I'll opt for the uppercase solution.
Jan 28 2014
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 1/28/14, Chris <wendlec 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.
Jan 28 2014
next sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
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?
Jan 28 2014
next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
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?
Jan 28 2014
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 1/28/14, 10:00 AM, "Ola Fosheim Grøstad" 
<ola.fosheim.grostad+dlang 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
Jan 28 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jan 28 2014
next sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
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?
Jan 28 2014
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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.
Jan 28 2014
parent reply "Chris" <wendlec tcd.ie> writes:
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 ;)
Jan 28 2014
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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.
Jan 29 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
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? ;)
Jan 29 2014
parent reply "Chris" <wendlec tcd.ie> writes:
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);
Jan 30 2014
parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Thursday, 30 January 2014 at 10:15:10 UTC, Chris wrote:

 offspring = document.createElement("div");

 document.torso.addOffspring(div);
Looks great! :D
Jan 30 2014
prev sibling parent reply "Martin Cejp" <minexew gmail.com> writes:
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.
Jan 28 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
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 :)
Jan 28 2014
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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...
Feb 01 2014
prev sibling next sibling parent reply "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail gmail.com> writes:
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++.
Jan 28 2014
parent "Chris" <wendlec tcd.ie> writes:
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.
Jan 28 2014
prev sibling parent "Piotrek" <a b.c> writes:
On Tuesday, 28 January 2014 at 12:57:02 UTC, Andrej Mitrovic 
wrote:
 On 1/28/14, Chris <wendlec 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.
http://d.puremagic.com/issues/show_bug.cgi?id=5775 Actually I'm still wainting for body release ;)
Jan 28 2014
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Jan 28 2014