dotnet / csharplang

The official repo for the design of the C# programming language
11.48k stars 1.02k forks source link

C# 8 - System.Index #2148

Closed Korporal closed 5 years ago

Korporal commented 5 years ago

I'm playing with VS 2019 and building a project with C# 8, however the use of ^is hampered because the compiler cant find System.Index. I have no idea where that is and it doesn't seem to be a nuget package. So am I able to use ^in VS 2019 yet or not?

HaloFour commented 5 years ago

@xCyborg

No that you bring it up, I like [-2] better than [^2]

Negative indexes already mean something in C# and in the CLR. This change would break a lot of existing code.

But there is still an inconsistency, in normal indices the first element is [0] so it stands to reason that the last element should be [^0] and not [^1]

The position before the first element is 0. The position after the last element is ^0. You can imagine the buffer as being circular, where 0 just wraps back around to ^0 (or -0, where it already literally means 0) except that you're not permitted to wrap around.

@popcatalin81

It is consistent, both with itself and with other languages. Willful ignorance of those explanations and of how indexing works does not change that reality.

popcatalin81 commented 5 years ago

@HaloFour There is no "Ignorance", it's people's opionions on this topic, repeating same thing as a broken record does not make it true. It's not consistent if indexes are offsets: Please look again:

[0] // First element, Offset 0 from start
[0..^0] // this works
[^0] // THIS Throws !!!
YairHalberstadt commented 5 years ago

.. Means up to but not including, so the is perfectly consistent

popcatalin81 commented 5 years ago

.. Means up to but not including, so the is perfectly consistent

[0..^0] So this means from first element up to 1 element past the end of the array but not including?

YairHalberstadt commented 5 years ago

Precisely. As HaloFour said, view the Array as circular.

HaloFour commented 5 years ago

@popcatalin81

There is no "Ignorance", it's people's opionions on this topic, repeating same thing as a broken record does not make it true.

How something was designed to work is not a question of opinion. You're either right or you're wrong. As many people have pointed out, including previous members of the C# team, you're wrong.

[0..^0] So this means from first element up to 1 element past the end of the array but not including?

Yes, just like [0..1] gets you only one element because it does not go past the latter index.

popcatalin81 commented 5 years ago

How something was designed to work is not a question of opinion.

@HaloFour ur I'm not "asking" how it was designed (and don't need explanations for that Thank you!) That's clear. I'm expressing an opinion "for" the design!

Please stop trying to suppress opinions you don't share.

popcatalin81 commented 5 years ago

[0..^0] So this means from first element up to 1 element past the end of the array but not including?

Yes, just like [0..1] gets you only one element because it does not go past the latter index.

The question was rhetorical. It was there to point out the ridiculousness of the definition.

HaloFour commented 5 years ago

The question was rhetorical. It was there to point out the ridiculousness of the definition.

There's nothing ridiculous about it. It's consistent with itself and decades of language design. You not liking it doesn't make it inconsistent.

If you want to argue these points Gitter is probably the more appropriate venue. This isn't going anywhere here.

popcatalin81 commented 5 years ago

If you want to argue these points Gitter is probably the more appropriate venue. This isn't going anywhere here.

Then stop suppressing other's opinions. You have yours, I've left mine to support other opinions. Leave it at that! Accept my opinion like other's is a point of view and don't use circular augments like:

"It's correct because it this is how it was designed. And it was designed like this because this is the correct way."

Korporal commented 5 years ago

@popcatalin81

Be warned, several regulars here react with hostility when their collective decision making is questioned. We - the great unwashed - are simply not worthy to question these gifted individuals.

Anyway, just wait until this is in full public release (which I presume will be next week ?), we're gonna see a host of articles and blog posts emerge by people who are going to be dismayed at the way this has been designed, it may even become as infamous as the Intel floating point bug!

YairHalberstadt commented 5 years ago

@Korporal None of the regulars here we're involved in that decision. The LDC made that decision, none of whom contributed to this discussion as far as I can see.

Korporal commented 5 years ago

@Korporal None of the regulars here we're involved in that decision. The LDC made that decision, none of whom contributed to this discussion as far as I can see.

@YairHalberstadt - Perhaps but they implicitly supported and today defend the design else we'd surely see their objections expressed here would we not? Never forget the adage "The only thing necessary for the triumph of evil is that good men should do nothing."

bigredd0087 commented 5 years ago

@popcatalin81

Be warned, several regulars here have presented that in their opinion the current design is not consistent. When someone else presents a logical argument as to why it is consistent these individuals react with hostility because their opinion is questioned.

jnm2 commented 5 years ago

After finding the point between elements naturally described by [x] or [^x], is the existing convention of reading forward inherent to the single-item indexing syntax itself ([ and ]), or is the convention flipped if the index passed to the indexer is a from-end index (^)?

I see pros and cons to both. No matter which one you pick, you will have to do by-one corrections in a large set of scenarios. I'm not sure which set is larger, but don't be mistaken and think that one of these choices makes off-by-one go away.

Also, each way of thinking about it has a powerful mnemonic that will keep you on the right track once this is part of the language and you have to deal which whichever way it is.

For the language design team's current choice:

If they change their minds:

I suspect I'll be doing less by-one math with the team's current choice because that's what I gather that their own conclusions were (from what I've read). But either way, I know I'll be able to emphasize the most useful mindset and make the most of it. (Because of this, I'm not really invested in the outcome. I'm more invested in fending off FUD by showing how usable each alternative really is.)

jnm2 commented 5 years ago

Another way to look at it which has been buried:

Zero-based indexing doesn't mean the first item is 0; it means that 0 is where that item starts and 1 is where it stops. 0.5 indicates that you've traveled halfway across the item. This isn't the counting intuition we learned as kids, but it's the only one that makes sense in the face of fractions. Beyond that consistency, it also immediately gets rid of having to remember to add and subtract 1 when measuring lengths.



hat



Don't think of it like a calendar; think of it like a tape measure.


0 means the first tick mark, having traveled across 0 characters from the beginning. ^0 means the last tick mark, having traveled across 0 characters from the end.

Then, 0..^0 takes the whole string. 0..1 collects a single character (into a string), just like str[0]. 0 and 1 are not referring to characters; they are referring to tick marks between the characters.

Our current indexers imply n..(n+1) when we say str[n]. str[6] throws since 6..(6+1) is past the end of the string.

agocke commented 5 years ago

If you want to understand our decision-making process on making Range open on the end, you should probably read through the language design notes, especially https://github.com/dotnet/csharplang/blob/72a4daa8532e191858f9f887f4c8d280ec006bb4/meetings/2018/LDM-2018-02-14.md

Korporal commented 5 years ago

Another way to look at it which has been buried:

Zero-based indexing doesn't mean the first item is 0; it means that 0 is where that item starts and 1 is where it stops. 0.5 indicates that you've traveled halfway across the item. This isn't the counting intuition we learned as kids, but it's the only one that makes sense in the face of fractions. Beyond that consistency, it also immediately gets rid of having to remember to add and subtract 1 when measuring lengths.

hat

Don't think of it like a calendar; think of it like a tape measure.

0 means the first tick mark, having traveled across 0 characters from the beginning. ^0 means the last tick mark, having traveled across 0 characters from the end. Then, 0..^0 takes the whole string. 0..1 collects a single character (into a string), just like str[0]. 0 and 1 are not referring to characters; they are referring to tick marks between the characters. Our current indexers imply n..(n+1) when we say str[n]. str[6] throws since 6..(6+1) is past the end of the string.

@jnm2 - I think the diagram is fundamentally flawed. The integers should sit beneath the cells not the left hand boundary lines, it's the cells (the individual bytes of the byte addressable memory) that are numbered not the implied "lines" between them.

HaloFour commented 5 years ago

@Korporal

I think the diagram is fundamentally flawed. The integers should sit beneath the cells not the left hand boundary lines, it's the cells (the individual bytes of the byte addressable memory) that are numbered not the implied "lines" between them.

Effectively it would be the same thing. 5 and ^1 would still index the same element, and both 6 and ^0 would be out of range.

Korporal commented 5 years ago

@Korporal

I think the diagram is fundamentally flawed. The integers should sit beneath the cells not the left hand boundary lines, it's the cells (the individual bytes of the byte addressable memory) that are numbered not the implied "lines" between them.

Effectively it would be the same thing. 5 and ^1 would still index the same element, and both 6 and ^0 would be out of range.

@HaloFour - But the diagram is flawed and does not correctly represent the problem, as soon as we begin to tolerate an inaccuracy like this it's effects begin to contaminate the solution.

CyrusNajmabadi commented 5 years ago

@HaloFour - But the diagram is flawed

The diagram is flawed for you. This conversation has happened numerous times now. It's your opinion and it's been heard. But it hasn't been accepted by others. Without a sufficient argument as to why it's more appropraite to see things your way, nothing is going to change here.

Korporal commented 5 years ago

@HaloFour - But the diagram is flawed

The diagram is flawed for you.

@CyrusNajmabadi - The diagram is flawed period because it numbers the bondaries between cells rather than the cells, no wonder you have trouble understanding when people express objections here.

This conversation has happened numerous times now. It's your opinion and it's been heard. But it hasn't been accepted by others. Without a sufficient argument as to why it's more appropraite to see things your way, nothing is going to change here.

Are you referring to my opinion about the diagram or the implementation of this index feature? please express yourself accurately if you want to engage in a meaningful technical discussion with me.

If you are referring to my views on the implementation then you must be corrected when you assert "It's your opinion and it's been heard. But it hasn't been accepted by others." because even a cursory reading of this issue will show you there are several other people expressing criticisms of how this has been implemented, if you took the trouble to remain objective you'd have noticed this rather than emotionally focusing only on what I say.

CyrusNajmabadi commented 5 years ago

@CyrusNajmabadi - The diagram is flawed period because it numbers the bondaries between cells rather than the cells, no wonder you have trouble understanding when people express objections here.

You are stating what it does, not why it is flawed. Others (including myself) do not feel there is any flaw with this sort of representation of the data/logic involved. Indeed, this is how i've been taught and have taught others. It's a very sensible way of understanding ranged operations. For example, when reading a segment of an array, one often is given a starting position and a length. These representations make it very clear waht's happening. The approach that has hte numbers under the slots makes it a lot less clear. Are reads happenign past that point or starting at that point? What does a 0 read length mean if the index selects the whole item? etc. etc.

If you are referring to my views on the implementation then you must be corrected when you assert "It's your opinion and it's been heard. But it hasn't been accepted by others." because even a cursory reading of this issue will show you there are several other people expressing criticisms of how this has been implemented,

yes. Your opinion has been heard. It is not agreed with. I'm not sure what you want or expect at this point. Continually harping that you don't like this formalization, while not providing adequate justification, won't get you anywhere.

CyrusNajmabadi commented 5 years ago

no wonder you have trouble understanding when people express objections here.

I have no trouble understanding. I'm simply stating that your objection has been heard and understood. But that it has also been rejected. The formalization around this has been discussed at length and the pros and cons of it are well understood (especially in relation to the formalization you want). The options were considered and this was felt to be the best approach with full knowledge of all the pros/cons that have since been listed and repeated ad-nauseam.

If you don't have anything further to add to the discussion, then simply reiterating that you don't like this formalization and prefer your own, won't get anywhere.

--

Note: one thing i did to help out here was to try to use both forms. I did a bunch of work using these alternative approaches working with strings, arrays, file buffers, and several Roslyn data types. By far, the easiest and most intuitive to wrap my head around was the [inclusive/exclusive) form. There were a couple of cases where it was slightly worse. But those were the rare cases.

This investigation was also done by several people working with the Range/Index proposal. And it was their great work at actually investigating many domains that helped push nearly the entire LDM (possibly the entire LDM, but i don't really recall at this point) to feel that this form was the better of the two to have by default.

jnm2 commented 5 years ago

@Korporal Saying the diagram is flawed is begging the question. Maybe it's flawed, if the only way to see things is from a single point of view. The diagram is really good at making the C# team's point of view more intuitive.

It's coming across as though you've prejudged the issue and aren't honestly trying to see things from any other point of view. Which is fine, but everyone can sense that you're doing this and practically speaking it's not a convincing maneuver. Neither is combativeness or aggressive choice of words. You may feel they are justified, but even if they are they're working directly against your ability to make a difference, so you decide.

Korporal commented 5 years ago

@jnm2

@Korporal Saying the diagram is flawed is begging the question. Maybe it's flawed, if the only way to see things is from a single point of view. The diagram is really good at making the C# team's point of view more intuitive.

How can saying the diagram is flawed be begging a question? it's either an accurate depiction of cells and how they're numbered or it isn't, why the reluctance to see the obvious here? If you think a sloppy inaccurate diagram is a sound basis upon which to have a technical discussion then that's fine but I don't work that way.

It's coming across as though you've prejudged the issue and aren't honestly trying to see things from any other point of view. Which is fine, but everyone can sense that you're doing this and practically speaking it's not a convincing maneuver. Neither is combativeness or aggressive choice of words. You may feel they are justified, but they're working directly against your ability to make a difference, so you decide.

You say all that just because I regard the diagram to be poor? Then you claim to know what "everyone" is able to "sense" whatever that means!

There is no aggression in anything I've posted and I resent you saying otherwise, I disagree on several points with respect to this language feature and as I've just said to someone else I am far from being alone in my criticisms.

Please stick to the subject and lets have less strawman posts please.

CyrusNajmabadi commented 5 years ago

How can saying the diagram is flawed be begging a question?

You're using your premise to support yourself. But your premise has not been established as being true. It's just a claim on your part.

CyrusNajmabadi commented 5 years ago

it's either an accurate depiction of cells and how they're numbered or it isn't

To many people here, it's accurate. It's certainly accurate to me, and it's a long established way of thinking about how data is laid out and how indexing and ranges work. It's not like this suddenly popped into existence in this thread.

If you think a sloppy inaccurate diagram

Begging the question again.

I disagree on several points with respect to this language feature

Your disagreement has been heard and evaluated. It was assessed against the other arguments made for the feature as is, along with the empirical evidence about the pros/cons of all the possible options present. It was rejected as providing a less intuitive and understandable model, as well as providing a system that was less easy to use in practice across a wide set of APIs.

Absent anything new you can add to help support your argument, nothing about your dislike of the existing POR will change anything.

theunrepentantgeek commented 5 years ago

@Korporal wrote:

There is no aggression in anything I've posted

The primary limitation of a forum like this is that we have only the written text to interpret. It's a low bandwidth medium. We don't get any additional information from tone of voice, body posture, pace of delivery, facial expression, etc. Add in the fact that different people use the English language in markedly different ways, and it becomes really easy for miscommunication to occur.

Hugh, I'm totally willing to believe that you read your words and don't see them as aggressive.

Sitting here, on my Saturday morning, most likely halfway around the world from you - because New Zealand is halfway around the world from most everywhere ... you do come across to me across as aggressive. Very much so.

That said, I have no doubt that we'd have a great deal of fun debating language design issues over a quiet drink or two, should the opportunity occur.

jnm2 commented 5 years ago

@Korporal What am I supposed to do with anything you just said? I'm trying to reach out and find middle ground, and you've just taken the opportunity to be rude to me again. Instead of giving an argument, you're sticking to flat contradictions and calling my example sloppy and inaccurate. What would you do if someone replied to everything you said by directly contradicting it, claiming to see a straw man in your argument, and finding a few degrading things to say about you?

I'm at a loss. The most reasonable thing I can think of right now, after this final appeal to your humanity, is for me to stop participating in this thread. I mean, what else can I do at this point? You win the stubbornness game, congrats! Nothing has been accomplished.

CyrusNajmabadi commented 5 years ago

There is no aggression in anything I've posted and I resent you saying otherwise,

The important thing to realize is that you have the ability to affect how others read your words. I've certainly not meant to be aggressive in the past, but it's been read that way by others. That's on me when, on the whole, that is the interpretation of many have come to the same interpretation.

In this case, you've come across extremely aggressive. You seem to have flat out been unwilling to come to any sort of understanding of positions different from your own, and you seem to have rejected the idea that your position has been considered but was overruled based on a careful analysis of hte information as well as empirical studies of hte ecosystem. Because you have been unwilling to try to reach any sort of common ground, and because you seem to just flat out reject different points of view, you come across extremely aggressive.

You may not intend that. But it's certainly how it's been coming across to me for nearly your entire conduct in this thread.

Korporal commented 5 years ago

@Korporal wrote:

There is no aggression in anything I've posted

The primary limitation of a forum like this is that we have only the written text to interpret. It's a low bandwidth medium. We don't get any additional information from tone of voice, body posture, pace of delivery, facial expression, etc. Add in the fact that different people use the English language in markedly different ways, and it becomes really easy for miscommunication to occur.

Hugh, I'm totally willing to believe that you read your words and don't see them as aggressive.

Sitting here, on my Saturday morning, most likely halfway around the world from you - because New Zealand is halfway around the world from most everywhere ... you do come across to me across as aggressive. Very much so.

That said, I have no doubt that we'd have a great deal of fun debating language design issues over a quiet drink or two, should the opportunity occur.

@theunrepentantgeek - Please quote me then, don't paraphrase - is this request too something you'd regard as aggressive?

Bevan you accuse me of being aggressive yet fail to actually quote an example of such from one of my posts. Perhaps you regard the act of disagreeing and arguing one's case as being aggressive...

Anyway I have work to do and the pettiness I'm seeing here is almost juvenile, have fun guys.

CyrusNajmabadi commented 5 years ago

Anyway I have work to do and the pettiness I'm seeing here is almost juvenile, have fun guys.

That's an example of aggressiveness, and not being willing to work toward a common ground or understanding.

Here are other examples:

Be warned, several regulars here react with hostility when their collective decision making is questioned. We - the great unwashed - are simply not worthy to question these gifted individuals.

The diagram is flawed period ... no wonder you have trouble understanding

why the reluctance to see the obvious here? If you think a sloppy inaccurate diagram is a sound basis upon which to have a technical discussion then that's fine but I don't work that way.

Honestly, 'aggressive' is putting it mildly. You're just not behaving in a manner that makes anyone want to converse with you.

Korporal commented 5 years ago

@CyrusNajmabadi - You clearly don't know what aggression is Cyrus, you must be of a rather delicate constitution if my words cause you that much angst.

Now here's an idea rather than repeatedly whining about my posts here and the effrontery you perceive when someone disagrees with you and is unimpressed with your responses, why not simply stick to the subject at hand.

Here's my take 1) The diagram is sloppy and that needs to be said and 2) the way this language feature has been implemented will go down in history as a poor design, of that I'm sure. If people can't even depict the mechanism accurately in diagrams how can we expect the ensuing designs to be sound?

CyrusNajmabadi commented 5 years ago

Cyrus, you must be of a rather delicate constitution if my words cause you that much angst.

This is another aggressive and unnecessary statement.

Now here's an idea rather than repeatedly whining about my posts here and the effrontery you perceive when someone disagrees with you and is unimpressed with your responses, why not simply stick to the subject at hand.

Because your behavior and attitude makes it so that people do not want to converse with you on this topic. If you are unwilling to do anything about that, then no progress can be made at all since any relevant parties will simply no longer respond to you.

Here's my take 1) The diagram is sloppy and that needs to be said

Yes, as stated before, your opinion here has been gathered, it just isn't something that is agreed with.

the way this language feature has been implemented will go down in history as a poor design, of that I'm sure.

Thanks for letting us know. That's a risk people are willing to take.

If people can't even depict the mechanism accurately in diagrams

The mechanism seems easy to depict in diagrams. You happen to not like the diagram, but that doesn't mean it's not simple to represent and explain.

HaloFour commented 5 years ago

Yes, everyone will be incredibly confused when C# happens to work the same way as every other language.

Korporal commented 5 years ago

@CyrusNajmabadi

Anyway I have work to do and the pettiness I'm seeing here is almost juvenile, have fun guys.

That's an example of aggressiveness, and not being willing to work toward a common ground or understanding.

It's helpful to review the definition of the term (you chose to use) "aggressiveness"

"Aggressiveness is rarely malicious or violent. Instead, the word connotes assertiveness and drive—a more figurative aggression. It is more of an attitude, and it may even be viewed as a positive character attribute in the workplace or on a sports field, for instance. Aggression involves behaviors that would not be considered acceptable in these settings." [emphasis mine]

From here. so thank you for the compliment.

I'm done in this thread now.

CyrusNajmabadi commented 5 years ago

why not simply stick to the subject at hand.

Every point about your position has already been addressed. You yourself have admitted it simply comes down to opinion about where you land on the right way to interpret things (i.e. Here's my take 1) The diagram is sloppy). So what else is there to say? Your position has not convinced people.

CyrusNajmabadi commented 5 years ago

I'm done in this thread now.

Thanks. I think removal from the thread will definitely help keep things more civil and on topic. I appreciate you seeing that and being willing to bow out.

agocke commented 5 years ago

I think the original question has been answered and the rational of why we chose the current design has been linked, so I'm closing this issue.