**@Swend**

> `class MyClass*:` results in an error "Error: expression expected, but found 
> ':'", and I haven't found a good way around it.

You actually can get that syntax to work like this:
    
    
    class Foo as object:
      ...
    
    class Bar* as ref object:
      ...
    

That said, it's annoying and there's one big flaw with using a class macro like 
that right now: interdependent types can only be defined in a type section. 
However, we can do OOP in Nim just fine in a more "Rust like" fashion. Eg:
    
    
    type
      Person = object
        name: string
        age: float
    
    impl Person:
      proc greet =
        echo "Hello, I am ", name, "."
        echo "I'm ", age, " years old."
    
    var bob = Person(name:"Bob", age:45)
    bob.greet()
    

This way has a couple of advantages over the `class` macro:

  * Uses standard type syntax.. so interdependent types work, and you don't 
need to deal with `*` marks.
  * You can define procedures from multiple modules with the `impl` macro.
  * You can extend the macro to do fancy things for your application. Eg, `impl 
MyButton as GuiItem`, `impl Player in Stage2.DangerZone`, etc..



Here's some working [example 
code.](https://gist.github.com/PhilipWitte/f4f0c18f7f6436ee87dc3c506343d859) of 
an `impl` macro. It needs to be improved a bit to support generics.. but it 
might help you start.

Reply via email to