Objectmapper: Define constant properties (let)

Created on 8 Jan 2016  ·  3Comments  ·  Source: tristanhimmelman/ObjectMapper

Hi.

I use Gson for mapping objects in Java. With Gson I can define constants values (final) for a given class, but I think it is not possible to do it using ObjectMapper, isn’t it?

I mean, can I define constant properties with “let” using ObjectMapper?

I think the fact it could not be possible is something to do with the language Swift itself, some kind of limitation, but I do not know exactly why.

But it is really frustrating to not be able to prevent mutability in my system in classes mapped with ObjectMapper, which are the majority!

And by the way, thank you for this great library ;)

Most helpful comment

I'd like to :+1: support for let properties. However there are a couple of workarounds:

  • Prefix your var declarations with private (set).
  • Use a type with value semantics (a struct) instead of reference semantics (class), and assign it to a let property. note that this has some side effects that may or may not be desirable.

All 3 comments

For the sake of simplicity I have chosen to not support immutable types in ObjectMapper for the time being. I have some experimental code in the project that supports it, however it is not fully tested and I am not recommending it's use at the moment. If you need immutable types there are other projects such as Argo which support it.

I'd like to :+1: support for let properties. However there are a couple of workarounds:

  • Prefix your var declarations with private (set).
  • Use a type with value semantics (a struct) instead of reference semantics (class), and assign it to a let property. note that this has some side effects that may or may not be desirable.

@frankus I just created an issue for this #383

Was this page helpful?
0 / 5 - 0 ratings