Use of _ as a placeholder #278
brotherbill
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Using _ as a placeholder was first introduced by functional languages such as Haskell.
The intention is that _ means discard or ignore. It is used with deconstruction of tuples, and for ignored parameters.
This allows lazy developers to avoid creating dummy variables just to satisfy the compiler, and leads to optimizations as deconstruction doesn't need to deconstruct all the values, which would then be ignored.
C# originally treated _ as just another variable name.
When the idea came up to treat _ as a discard symbol, it had to be backward compatible and not break existing code.
C# will check if it is being used as a variable name, and if so, will continue to do so.
Otherwise it will treat _ as a discard symbol.
The issue is that when developers use _ as a variable, that may be confusing, if the reader assumes it means discard.
The reader then needs to realize that _ is a variable name, not a discard symbol.
Example:
In this case, _ is an integer variable name passed to new CarouselItem(_).
This would be better, because it is obvious that i is being passed to new CarouselItem(i)
Beta Was this translation helpful? Give feedback.
All reactions