Thread overview
Tagged enums why reserved words are not permitted ?
Oct 28, 2014
Domingo
Oct 28, 2014
Jonathan M Davis
Oct 28, 2014
Jacob Carlborg
Oct 28, 2014
Gary Willoughby
Oct 28, 2014
Jonathan M Davis
Oct 28, 2014
ketmar
Oct 28, 2014
H. S. Teoh
October 28, 2014
Hello !

I'm not sure if I'm missing something here but for a tagged enum it doesn't seem to make sense to forbid reserved keywords like:

enum CrudOps {read, write, delete}

The dmd compiler are complaining:
------
cte.d(4): Error: basic type expected, not delete
cte.d(4): Error: no identifier for declarator int
cte.d(4): Error: type only allowed if anonymous enum and no enum type
cte.d(4): Error: if type, there must be an initializer
cte.d(4): Error: found 'delete' when expecting ','
------

It doesn't make sense to me because this kind of enum will not polute the global space and always need to beused with the tag: CrudOps.delete

I'm missing something here ?

Cheers !
October 28, 2014
On Tuesday, October 28, 2014 00:31:43 Domingo via Digitalmars-d-learn wrote:
> Hello !
>
> I'm not sure if I'm missing something here but for a tagged enum it doesn't seem to make sense to forbid reserved keywords like:
>
> enum CrudOps {read, write, delete}
>
> The dmd compiler are complaining:
> ------
> cte.d(4): Error: basic type expected, not delete
> cte.d(4): Error: no identifier for declarator int
> cte.d(4): Error: type only allowed if anonymous enum and no enum
> type
> cte.d(4): Error: if type, there must be an initializer
> cte.d(4): Error: found 'delete' when expecting ','
> ------
>
> It doesn't make sense to me because this kind of enum will not polute the global space and always need to beused with the tag: CrudOps.delete
>
> I'm missing something here ?

keywords are always keywords wherever they are in the code and are only legal where those keywords are legal. I can see how you could come to the conclusion that the compiler shouldn't think that you're trying to use a keyword as an enum member, but the compiler does not take that into account. And I've never seen a language where it did (though one may exist out there somewhere). The thing that's been done in Phobos in this type of situation is to put an underscore on the end of the keyword, so you'd get

enum CrudOps { read, write, delete_ }

and while that may not be what you want, it's pretty much the best that you can do.

- Jonathan M Davis

October 28, 2014
On Tue, 28 Oct 2014 00:31:43 +0000
Domingo via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> Hello !
> 
> I'm not sure if I'm missing something here but for a tagged enum it doesn't seem to make sense to forbid reserved keywords like:
> 
> enum CrudOps {read, write, delete}
> 
> The dmd compiler are complaining:
> ------
> cte.d(4): Error: basic type expected, not delete
> cte.d(4): Error: no identifier for declarator int
> cte.d(4): Error: type only allowed if anonymous enum and no enum
> type
> cte.d(4): Error: if type, there must be an initializer
> cte.d(4): Error: found 'delete' when expecting ','
> ------
> 
> It doesn't make sense to me because this kind of enum will not polute the global space and always need to beused with the tag: CrudOps.delete
> 
> I'm missing something here ?
> 
> Cheers !
D has no "context keywords". i.e. no keyword can be used as identifier, even if it's possible to distinguish between use cases. you can't, for example, use "body" identifier for variable/field name (and this annoys me alot).

i believe that this was done so source code analyzing tools can stop tracking context while parsing D source. i.e. tool is not required to do semantic analysis to determine if "body" is identifier or function body start.

i don't think that this will change. and this is the reason, for example, for adding "@property", "@safe", "@trusted" and so on instead of "property", "safe", "trusted": D taken some valuable keywords already and nobody wants to lose even more names. ;-)


October 28, 2014
On Tue, Oct 28, 2014 at 12:31:43AM +0000, Domingo via Digitalmars-d-learn wrote:
> Hello !
> 
> I'm not sure if I'm missing something here but for a tagged enum it doesn't seem to make sense to forbid reserved keywords like:
> 
> enum CrudOps {read, write, delete}
> 
> The dmd compiler are complaining:
> ------
> cte.d(4): Error: basic type expected, not delete
> cte.d(4): Error: no identifier for declarator int
> cte.d(4): Error: type only allowed if anonymous enum and no enum type
> cte.d(4): Error: if type, there must be an initializer
> cte.d(4): Error: found 'delete' when expecting ','
> ------
> 
> It doesn't make sense to me because this kind of enum will not polute the global space and always need to beused with the tag: CrudOps.delete
[...]

Not true:

	CrudOps x;
	with (CrudOps) {
		x = read;
		... // etc.
	}


T

-- 
In a world without fences, who needs Windows and Gates? -- Christian Surchi
October 28, 2014
On 2014-10-28 01:51, Jonathan M Davis via Digitalmars-d-learn wrote:

> And I've never seen a language where it did (though one may exist out there somewhere)

Ruby:

class Foo
end

Foo == Foo.new.class # perfectly legal

You always need to have a receiver when calling the "class" method. This is not uncommon at all, it's needed (or one way to do it) when accessing class methods (static methods) from inside of a method

class Bar
  def self.foo # class/static method
  end

  def baz
  end

  def bar
    baz
    self.class.bar # need to have an explicit receiver when calling "class"
  end
end

In fact, in Ruby you can use any name for a method, but you might not be able to declare it, or call it :). Instead one can use metaprogramming and reflection:

class Bar
  define_method("foo bar") do # declares the method "foo bar"
    puts "calling foo bar"
  end
end

Bar.new.send("foo bar") # calls the method "foo bar"

CoffeeScript:

class Foo
  bar: ->
    console.log "bar"

a = { class: "foo" }
console.log a.class # prints "foo"

-- 
/Jacob Carlborg
October 28, 2014
On 10/27/14 8:31 PM, Domingo wrote:
> Hello !
>
> I'm not sure if I'm missing something here but for a tagged enum it
> doesn't seem to make sense to forbid reserved keywords like:
>
> enum CrudOps {read, write, delete}
>
> The dmd compiler are complaining:
> ------
> cte.d(4): Error: basic type expected, not delete
> cte.d(4): Error: no identifier for declarator int
> cte.d(4): Error: type only allowed if anonymous enum and no enum type
> cte.d(4): Error: if type, there must be an initializer
> cte.d(4): Error: found 'delete' when expecting ','
> ------
>
> It doesn't make sense to me because this kind of enum will not polute
> the global space and always need to beused with the tag: CrudOps.delete
>
> I'm missing something here ?
>
> Cheers !

http://en.wikipedia.org/wiki/Reserved_word

"Making keywords be reserved words makes lexing easier, as a string of characters will unambiguously be either a keyword or an identifier, without depending on context"

-Steve
October 28, 2014
On Tuesday, 28 October 2014 at 00:51:17 UTC, Jonathan M Davis via Digitalmars-d-learn
> The
> thing that's been done in Phobos in this type of situation is to put an
> underscore on the end of the keyword, so you'd get
>
> enum CrudOps { read, write, delete_ }
>
> and while that may not be what you want, it's pretty much the best that you
> can do.
>
> - Jonathan M Davis

This is also mentioned in the style guide too.

http://dlang.org/dstyle.html
October 28, 2014
On Tuesday, October 28, 2014 16:58:50 Gary Willoughby via Digitalmars-d-learn wrote:
> On Tuesday, 28 October 2014 at 00:51:17 UTC, Jonathan M Davis via Digitalmars-d-learn
>
> > The
> > thing that's been done in Phobos in this type of situation is
> > to put an
> > underscore on the end of the keyword, so you'd get
> >
> > enum CrudOps { read, write, delete_ }
> >
> > and while that may not be what you want, it's pretty much the
> > best that you
> > can do.
> >
> > - Jonathan M Davis
>
> This is also mentioned in the style guide too.
>
> http://dlang.org/dstyle.html

So, it is. I'd forgotten about that (even though I'm almost certainly the one who put it there). But that's what we decided on when it came up in std.traits a while back. It seemed like the only good way to use a keyword as an identifier without breaking the naming conventions by doing something like capitalizing it differently.

- Jonathan M Davis