-
Notifications
You must be signed in to change notification settings - Fork 4.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
Support delete member in C# dynamic object #16321
Comments
|
Well thank you. I forgot Then how about
|
Honestly if a language feature were to be added specifically to permit this behavior I'd prefer that void Main()
{
dynamic obj = LoadJson("config.json");
delete obj.MustDeleteData;
obj.NullableData = null;
SaveJson("config.json",obj);
} At the same time I'm not aware of a direct way in C# to set a property to void Main()
{
dynamic obj = LoadJson("config.json");
delete obj.MustDeleteData;
delete obj["MustAlsoDeleteData"];
obj.NullableData = null;
obj.UndefinedData = undefined;
SaveJson("config.json",obj);
} Although perhaps both could be accomplished through static members exposed on some helper class which would negotiate the dynamic object and manually manipulate it. I'm not sure that would be possible with the void Main()
{
dynamic obj = LoadJson("config.json");
DynamicHelper.delete(obj, "MustDeleteData");
obj.NullableData = null;
obj.UndefinedData = DynamicHelper.undefined;
SaveJson("config.json",obj);
} With Note that I'm aware that |
I think So I just think ~ is more safe to use. It like a shorthand to your |
Which is why I said they could be contextual keywords, as in the kind that aren't keywords if a variable or type of that name is in scope.
|
I'm baffled why would you use |
How structured is it if he's deleting a property? 😆 |
@jnm2 The question should be why it isn't structured? if something is removed from it I'd argue there's some garbage that shouldn't be there at all. p.s. The config.json file is probably structured and you can create a structure that represents it. |
@eyalsk Suppose the structure by design isn't known at compile time? |
@jnm2 Here are few examples/ideas:
Then when you serialize it you can ignore the Alternatively you can create a wrapper around the dynamic object.
Usage:
|
As I mentioned above, the ability to cast |
Are you speaking in general or in this specific scenario? because here he/she loads something that seems to be well known at compile-time. @HaloFour You're right it's implementation dependent but then you can still have the same wrapper and then instead of calling Wouldn't a contextual keyword that deletes the property would have to be implementation dependent in the same way? I mean it will have to go to this JavaScript engine and call delete to really remove it from the object, isn't? |
No, the DLR API has support for deleting members. C# just lacks a mechanism to expose using it. |
@HaloFour Okay, I didn't know that, now I do. 😆 |
From my perspective, a delete language concept seems too fundamental to implement just for the sake of |
BTW, didn't realize the OP speaks about API that available to |
@jnm2 I think opposite. So a keyword for the sake of dynamic is more important than LINQ But I agree that if there are |
If we could use contextual keyword there I think reuse void Main()
{
dynamic obj = LoadJson("config.json");
remove obj.MustDeleteData;
obj.NullableData = null;
SaveJson("config.json",obj);
} Also there are one thing I would like to, that void Main()
{
dynamic obj = new DynamicObject();
obj.Number = 1;
int i = remove obj.Number; // i became 1 and Number was deleted from obj
} If it possible I would like to have this behaviour on void Main()
{
Dictionary<string,int> dict = new Dictionary<string,int>();
dict["Number"] = 1;
int i = remove dict["Number"]; // i became 1 and Number was deleted from dict
} |
Probably to you. :) I'm not in favour of adding anything to dynamic because I don't use it as much, the only place I used it is in ASP.NET MVC 3 or 4 can't remember, I really prefer features added to LINQ any day...
It might make sense for |
@eyalsk We don't really need linq keyword because we could access linq in extension method style. Even we add more feature to linq it more likely is extension method anyway But
I think it like we have linq keyword. So it the same for But yeah this is so very minor |
You misunderstood me, I didn't say I want them to introduce the LINQ isn't just a bunch of extension methods, there are types related to it, expressions, language integration, etc...
Maybe it's really needed, let's assume it is, the question is how many gonna need it?
Don't you mean |
@eyalsk Almost all about linq is extension method on And linq expressions And what is language integration you mean? Linq is extension method so it is the same class citizen as all other in C#
Almost all dynamic object used for interop with dynamic language is needed. Well I think in present day we do so many workaround like
Right |
@eyalsk My point is, any linq feature mostly could be added in dotnet/corefx And don't need to add anything in dotnet/roslyn, unless you need to add keyword, which I don't think it need anyway Or are there any feature you really want on linq that cannot extend by extension method? But to expose delete on dynamic is another story. There are no clean way to do beside changing compiler |
Just because you think it's not needed doesn't mean it doesn't exist as part of the language, you can check the spec it's mentioned under Query Expressions.
Why? can't you just do this:
|
I don't really need anything atm but there are many issues about people asking more features added to LINQ: #15619 These are just some features but there are many more from syntactic sugars, adding existing language features that currently don't play well with LINQ to performance so it's not just about adding functionality. |
I don't think keyword is necessary. Why not just update BCL to have something like this? public static class DynamicHelper
{
public static void RemoveMember(object dynamicObject, string memberName);
public static bool TryRemoveMember(object dynamicObject, string memberName, out object removedMember);
} Its by far more simple solution to implement (if I understood correctly, it can be implemented directly in CLR without changes to any spec). |
The code example you write is dynamic person = new JObject();
person.Name = "John";
Console.WriteLine(person.Name); Yes, this is what is should be, but then how can you delete Name after that? dynamic person = new JObject();
person.Name = "John";
Console.WriteLine(person.Name);
((JObject)person).Remove("Name"); That's why I want to propose that it should be just dynamic person = new JObject();
person.Name = "John";
Console.WriteLine(person.Name);
remove person.Name; To make dynamic flow can complete by itself. That's what I mean |
Almost all that was needed to extend corefx. Some is just waiting C#7 for return ref or tuple. Nothing directly relate to linq anymore except 2 request for adding more keyword just for linq which I think is bad idea |
@zippec That's not a clean way to do. What you said is like, class is syntactic sugar, we don't need class syntax at all. We could workaround by making all function has Currently now we can workaround with anything, that's true, and it's not a blocking issue |
@Thaina If anything the keyword should probably be
So adding keywords to a common feature such as LINQ is in your opinion bad but adding a keyword to an uncommon feature is better? |
I think that a generic |
@jnm2 In C++ the How many people use
Maybe but there's some debatable questions to ask:
Check this #161. What do you mean by deterministic freeing? do you mean something like
Well, the use-case is very different because it actually related to #161 as noted there so there is a compelling reason to add it whereas here it seems reasonable but again how many people are going to use it? |
More like calling the finalizer, but to answer all your questions: I'm not making a proposal, I'm trying to get people's imaginations going and see if there is anything that |
@jnm2 Yeah I figured, I just thought to bring some more points to the discussion when considering the merits of this feature/keyword. :) |
I was thinking about using it with any collections. And idea that was popping up is, we should introduce public interface IHasIndexer<K,V> : ICollection<T>
{
this[K key] { get; set; remove; }
}
System.Collection.Generic.List<T> : .....,IHasIndexer<int,T>
{
....
public this[int key]
{
get {...}
set {...}
remove { this.Remove(key); }
}
}
System.Collection.Generic.Dictionary<K,V> : .....,IHasIndexer<K,V>
{
....
public this[K key]
{
get {...}
set {...}
remove { this.Remove(key); }
}
}
...
remove list[0];
remove dict["key"]; Something like this. To use Maybe this could be used on property too. If we have property that actually read things from other collection class A
{
....
Dictionary<string,int> serializeJsonMap;
public int Removable
{
get { return serializeJsonMap["X"]; }
set { serializeJsonMap["X"] = value; }
remove { remove serializeJsonMap["X"]; }
}
}
var a = new A();
remove a.Removable; |
@Thaina The call to the Why would you want to do |
I don't like the way compiler guessing Now the difference between Most of the times I use |
Yes, fortunately it doesn't have to guess anything.
I.. I don't know what to say.. you want to introduce such complexity just to replace a dot with a space!? and the funny part is that you actually believe that you can compare it to LINQ.
Let me tell you how this reads: Most of the time I drive the car therefor I want to have the bus station near my house which doesn't make sense at all because they are completely unrelated not to mention being a reason for one of them to exist. |
Then could you say why we have I am the one who should said I don't know what to say for people who just cannot grow out of sql language and work with functional programming normally to the point that need to add Your argument that you throw at me, all of that point to linq keyword in my perspective. Just because you want to transform collection with While what I was propose is to add convention standard to remove something. Like |
@eyalsk Also the problem using That's why I call it ps. |
A piece of the puzzle might be insignificant on its own but the whole puzzle can make it worthwhile. |
@eyalsk Then same go for The whole picture to have ps. I myself would not ever get the whole puzzle of linq keyword because I always prefer functional linq syntax over SQL |
@Thaina LINQ and SQL have different syntax and because both of them are declarative query languages they just happen to share some keywords but that's all. Anyway, I feel like this discussion is a bit silly so we'll have to agree to disagree. 😄 |
@eyalsk Yes I think it silly from the start that you make argument without even knowing |
@Thaina No, it's silly to point out a mistake after someone already admitted it, it's silly to hang on to someone's mistake and use it as an argument in the absence of one. |
@eyalsk I want you to know that your argument from the start make me feel it is silly argument. You admit it after does not make the past silliness get away Just as I say I think it silly from the start, I mean till now I was feel all of your arguments is silly. I just don't say it because it rude. So I was trying to be reasoning about it as much as I can But you say it out by yourself make me feel I don't need to bear it |
@Thaina I've noticed that it frequently happens in this kind of conversation that it seems to a person such as yourself that the others are being silly, when in fact they are being reasonable if you take a different perspective. @eyalsk's core argument is a good one, as is yours. Let's get away from the fringe metadiscussion. |
@jnm2 I know it that's why I never start saying the word silly. I always try to understand perspective of other people As I said I'm not start using word silly even I always feel it, I want to try to reasoning with it and make reasonable argument Just that, whenever someone start using it, I too want to be honest about my feeling too So, what I really want to say is, you should tell the person who start using it, not me |
@Thaina I DIDN'T say you are silly, I DIDN'T say your arguments are silly, I said that THE DISCUSSION is silly because it leads to nothing! |
moved dotnet/csharplang#143 |
Since
DynamicObject
supportTryDeleteMember
and dynamic object mostly used to bind with json and other remote system which sometimes treatnull
andnot exist
differentlyI think we should support
delete
syntax on dynamic objectmaybe just use
~
The text was updated successfully, but these errors were encountered: