6

I define a C++ interface with djinni:

member = interface +c {
    get_id(): string;
    get_name(): string;
}

My inherited implementation uses const getters, i.e.

class MyMemeber: public Member {
  private:
    string id;
    string name;
  public:
    string get_id() const override { return id; }
    string get_name() const override { return name; }
}

This obviously fails to compile, because of const attribute. Can I teach djinni to generate the base interface with const getters, too?

Lukas Würzburger
  • 6,543
  • 7
  • 41
  • 75
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307

1 Answers1

7

It is nowhere documented though from the very beginning djinni generates const methods (line 295 of src/source/CppGenerator.scala). Just add const in front of method signature in idl file:

member = interface +c {
    const get_id(): string;
    const get_name(): string;
}

I think it would be good idea to pull request tests for this feature (and some docs), currently only const fields are tested.

mkk
  • 675
  • 6
  • 17
  • 5
    Fascinating. I'm the primary maintainer of Djinni these days, and I didn't know about this feature, and even had it on a wish-list/backlog. It's only minimally useful since constness is meaningless to Java and ObjC, and all interface arguments are passed by pointer-to-non-const. Ideally what I'd really like is proper const-correctness, which would require const interface arguments, and some sort of enforcement in other languages. I'll add a const method to the test suite, at least, to demonstrate that it exists. – Andrew Apr 19 '16 at 02:49
  • 1
    @atwyman I was also surprised, thought that there is no such feature but wanted to verify it in source generator and voila! (-; Someone should verify if we don't have more stuff like this. (-; – mkk Apr 19 '16 at 06:34
  • I am glad I didn't start with the source code: it would result in me using this feature which never was properly tested, while nobody else knows that it exists! – Alex Cohn Apr 19 '16 at 16:29
  • 1
    I added a test and it works fine. I also added some docs to the README. Both should land on github this week. – Andrew Apr 19 '16 at 18:33
  • @AlexCohn I hope i did not mislead you. I think that my answer will require update when atwyman will push changes to git. Do you require any other updates in order to accept this answer? – mkk Apr 19 '16 at 22:45
  • Thank you **@atwyman** and **mkk** this was a great example of best advantages of Stack Overflow, the best in my experience. – Alex Cohn Apr 20 '16 at 08:18