Attila Kovacs 629 Posted February 26, 2021 Do we have a working lib/class which implements TNothingable<T> in Delphi? Where "null" is a valid value. For simple types I could achieve that relative easily, for structured types it's funnier to implement. Share this post Link to post
David Heffernan 2345 Posted February 26, 2021 You mean a nullable type? There are lots of implementations. Spring has one but there are plenty more. Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 No, one step above. Nothing / (values incl. null) Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 Actually Nullable<Nullable<T>> with a distinction by level between the nulls Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 (edited) as an example: T=type y: TNothingable<TArray<string>>; z: TNothingable<TSomeObject>; end; T.tojson => {} T.y := []; T.y.SetNull; T.tojson => { "y" : null } T.z := nil; T.tojson => { "y": null, "z": null } There are API's where I have to pass null values if I want to clear its content and I also want to keep everything mapped. I'm not willing to map those fields with string literals and build up a json-object. Edited February 26, 2021 by Attila Kovacs Share this post Link to post
Fritzew 51 Posted February 26, 2021 What is the usecase? sorry for asking, but I want to understand.... Share this post Link to post
Stefan Glienke 2002 Posted February 26, 2021 Your json is wrong - an empty array is represented as [], not as null Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 @Stefan Glienke Please tell this the people who are writing the php API's. I did the same with objects and passed "{}" and the server created the object with default values. @Fritzew A simple webshop php API, but any json API's out there where the values are interpreted individually. Share this post Link to post
Stefan Glienke 2002 Posted February 26, 2021 1 minute ago, Attila Kovacs said: @Stefan Glienke Please tell this the people who are writing the php API's. I did the same with objects and passed "{}" and the server created the object with default values. Which is correct, because {} represents an empty object, which is not the same as no object (null). Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 (edited) If you are telling me that you can't check a json array in php if it's holding a value of null, just if it's empty, then I'm fine, the above example is wrong. I'm not sure. Bollock what I wrote. It's just a key/value pair. There is no such thing that an array can't be null. This is the problem. Edit: My example above is indeed wrong on arrays. It should have been t.y.SetNull or similar. [] should translate to [], you are right. Edited February 26, 2021 by Attila Kovacs Share this post Link to post
Stefan Glienke 2002 Posted February 26, 2021 Last time I checked this was a Delphi forum, so no clue if php can do this and the API can very well be not 100% adhering the spec - but an array with a null in json would be [null] Anyway none of that relates to your initial question imo - all mentioned cases can be handled with default data types - explicit nullable type is to add that additional state of nothing/null/nada to a value type. Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 Yes, i did the same, returning TNothing = interface end; in the TValue. I just lost my temper on the structured types and I thought I'm asking if somebody has something similar (and maybe much simpler/better) done. Share this post Link to post
Attila Kovacs 629 Posted February 26, 2021 Ooookay, this conversation brought me to a moment of enlightenment. We were both right, but they are two different things (null/[]) so I will threat them differently, with the same JSONName attribute. This solves everything for now. I can keep my object mapping, which was the most important thing for me. Thx guys! Share this post Link to post
Rollo62 536 Posted February 27, 2021 Maybe you talk about TEmptiness<T> ? 🙂 Share this post Link to post
Attila Kovacs 629 Posted February 27, 2021 @Rollo62 Sometimes I have no Idea what am I talking about. Saint Augustine, on being asked what is time: "I know what it is, but when you ask me I don’t." 1 Share this post Link to post
Rollo62 536 Posted February 27, 2021 Don't worry, emptiness is full of everything, thats what physicist's know today 1 Share this post Link to post