-
Notifications
You must be signed in to change notification settings - Fork 1k
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
JSON-Style initializer syntax for dictionaries #2247
Comments
C#'s existing initializer syntax is completely agnostic to the data type in question. Collections with an var StdGrades = new JSON<int>()
{
{ Adam, 90 }
{ John, 60 }
}; Collections with an initializer can be called as you described. To add your syntax would mean a third syntax which seemingly takes a hard dependency |
@YairHalberstadt @HaloFour |
I think there's a seperate issue here which may be relevant, which is intellisense for dynamic objects. Using Roslyn with some sort of attribute language to indicate that a parameter to this method is the name of a property might make for an interesting experiment. It would be similar to the nullable flow analysis and associated attribute language API. |
You will have errors because no Adam and John variables in context. You can't change this behavior because it will break existing code. So, you must use:
and this can allow:
Which can't be used as properties names. |
@MohammadHamdyGhanem
The concept has been discussed. |
@YairHalberstadt |
This is a new angle :)
There the syntax uses "" for both keys and string values! This doesn't fit the purpose of my proposal. |
JSON requires that keys be strings. If they're not strings you get into the whole mess of what is a valid identifier and how do you escape invalid identifiers. JavaScript has a long list of reserved keywords which would make illegal identifiers. C# has a different list of reserved keywords. So, your flavor of the existing considered, prototyped and rejected proposal is considerably worse. |
Why not? Both of those are totally legal json property names: I should know. I wrote the json LS support for both TypeScript and Roslyn :) You can read the relevant RFC here: https://tools.ietf.org/html/rfc8259#section-4 Specifically, an object member is simply: So, any json-string is the only thing allowed as a member name in json. |
You don't need a language feature for this. You could just write a Roslyn completion provider that provides better intellisense for Json.net interactions. |
@HaloFour @CyrusNajmabadi |
@MohammadHamdyGhanem
There's no reason to have yet-another-style, and this is not a race to see how short if could possibly be. You can't possibly wade into using identifiers for string keys without dealing with the problem with identifiers.
No you didn't. Intellisense can certainly be improved to allow autocomplete of indexers. It would be quite literally the same work in order to enable autocomplete of these "pseudo-members". Also, dotnet/roslyn#3555 |
@MohammadHamdyGhanem Your very first sentence says this:
If you want to make working with a string-dictionary easier, then your solution better handle all strings that could go in a dictionary. |
@CyrusNajmabadi |
@MohammadHamdyGhanem
That has nothing to do with the language JavaScript or the JSON format. |
Easier in what way? Currently, you've only mentioned intellisense. But that's something you can improve upon without needing to change the language. |
@CyrusNajmabadi |
Be the change you want to see in the world :) If you want this to happen, feel free to contribute a PR. I'm happy to help advise you on how to get it done. But changing the language in order to get better intellisense is not really going to go anywhere. It'll be roughly 1000x more expensive than just making the PR to improve intellisense itself. |
I don't think it worth to implement this var StdGrades = new JSON<int>() {
Adam: 90,
John: 60
}; Just for cutting a little var StdGrades = new JSON<int>() {
["Adam"] = 90,
["John"] = 60
}; Which is already work, and much more versatile. It could work with not only JSON but also any kind of dictionary. And also it could take variable value, not just constant, with the one syntax. Your proposal is not general enough to be worth as a syntax And what will happen to this below code? var StdGrades = new JSON<int>() {
Count: 90
}; |
No, please read the rest of the proposal
|
@MohammadHamdyGhanem This is not related to the intellisense at all. We could just create smart intellisense that will populate possible value on indexer instead. And what you want is more like intellisense for
|
@Thaina |
And one gain over expando object, is that we can definr the type of the values rather than being objects. |
I already showed how intellisense can understand and support full string keys. I literally implemented that for TypeScript. That was in this post: #2247 (comment) :) As mentioned above, if you want intellisense to be improved, there are ways you can already do that today. I'd be happy to walk you through the process if you're interested. However, no language feature is needed for this currently. |
I want to access the keys by the dot notation syntax ( |
given that strings can contain far more allowed data than what is legal for an identifier name, your proposal seems far too limiting given the expressly stated goal of:
I don't see anything strange about that.
The language isn't going to change just because you don't like some piece of syntax. |
According to my proposal, they can't! This is why I proposed to have a special string-key generic value dictionary. If the name JSON cinfuses you, give it any other name. My focus is on the idea. JSON is the source where I got the idea, but this proposal is not about copying JSON as it is to .NET.
|
@MohammadHamdyGhanem
You can't stop it. void AddBad<T>(T dict) where T : IDictionary<string, int> {
dict.Add("bad identifier!", 123);
}
var json = new JSON<int>();
AddBad(json); The definition/rules for identifiers changes between every language.
Only if there is consensus that the change is a considerable improvement, especially to justify the exorbitant cost that goes into changing the language.
How many such features exist in C#? Language design is exceptionally conservative specifically for this reason. This kind of language feature was already considered for C# 6.0. It was roundly rejected on the basis that it added nothing worthwhile to the language and the problem of discovery of keys via Intellisense could be solved in more universally applicable ways.
No it couldn't, they two mean different things: Imports System
Imports System.Collections.Generic
Module Program
Sub Main()
Dim d As New Dictionary(Of String, Integer)
d.Add("Count", 123)
Console.WriteLine(d.Count) ' prints 1
Console.WriteLine(d!Count) ' prints 123
End Sub
End Module |
@HaloFour
This is a quick alternative to defining these keys and values as resources. |
@MohammadHamdyGhanem So trying to add to the dictionary would then throw at runtime? What's the point then? Use an anonymous type. |
@HaloFour |
@MohammadHamdyGhanem
Which means people can add invalid keys to it after the fact, as I said.
Only with keys that are valid identifiers, otherwise you have to resort back to the normal property syntax. This is exactly what makes it almost completely pointless. The
It doesn't help write code. It makes a very narrow case very slightly less verbose. This was already proposed and was already rejected. |
CLosing as the OP is no longer on github, and this was fairly poorly received |
JSON initializer for dictionaries
I suggest to make use of JSON syntax to make dealing with string dictionaries easier and less error prone.
At First, suppose we have this Dictionary:
This JSON dictionary has string keys, so we can write this special syntax to initialize it:
The special syntax using : grantees that the names at the left hand are the string keys, not names of variables, So, that syntax is the shorthand of:
This special syntax also is useful to tell the Intelligence that these key names can be used as if they properties of the JSON object:
If the compiler found that StdGrades has no properties with the names Adam and John, then it will check if these names are defined keys in the JSON initializer, so it can translate that code to:
The most benefit of this, is to provide intellisense to auto-complete the key names, so prevent typo mistakes, which are treated as new keys!
Note: This is also applied to VB, either with the . syntax or with the ! syntax.
Finally: JSON object will not be dynamic, so , we can't add new keys with the . syntax, because this will revert us back to the possible typo mistakes. New keys can only be added by the indexer syntax, or if they are will known from the start, they should be added in the JSON initializer.
Note:
Expando object can be use the same intializer to add some of intelligence for the dynamic properties.
The text was updated successfully, but these errors were encountered: