Tagged enums why reserved words are not permitted ?

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 28 00:18:09 PDT 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


More information about the Digitalmars-d-learn mailing list