-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle TomlLazy? #44
Comments
Right after posting this, I found that I could check with |
Hi, thanks for the question! Glad you managed to find the solution to your question.
var toml = new TomlTable();
toml["array"][0] = "val1"; // toml["array"] returns a TomlLazy instance, but index access [0] automatically replaces TomlLazy with TomlArray
toml["array"][1] = "val2"; // toml["array"] now returns a TomlArray The basic case is not really interesting, but the TomlLazy becomes useful when you use property initializiers, allowing you to construct the entire table in JSON-like syntax: var toml = new TomlTable
{
["owner"] = // Here TomlLazy is implicitly created
{
["name"] = "Foo", // Here, TomlLazy is implicitly replaced with a TomlTable
["dob"] = "bar",
},
["array"] = { [0] = 1, [1] = 2 } // Same thing, but now TomlArray is created because of index access
}; This pattern was originally taken from SimpleJSON which at some point was a fairly popular JSON parser/writer for Unity games back when C# did not have. Nowadays, I'd say that this implicit behaviour is somewhat cryptic, and it belongs to a separate library. Alas, I think it's a breaking change to remove it. PS: |
From the README and the code I can't quite surmise what
TomlLazy
is actually used for, but in my tests it seems that if I try to access a key that doesn't exist in a table, aTomlLazy
is returned instead. Is there a way I can check if a key doesn't exist so I can handle that?The text was updated successfully, but these errors were encountered: