3 ms·
An underscore is primarily used as an identifier to denote a variable/parameter whose name does not really matter. It's more commonly used when you're not plann
by mostlylurks 6y ago
An underscore is primarily used as an identifier to denote a variable/parameter whose name does not really matter. It's more commonly used when you're not planning on using the aforementioned variable, but it's also sometimes used when the identifier is used but its name doesn't matter.
One such instance is the abbreviated scala lambda syntax, where
(x:Int) => x + 2
can be abbreviated to
_ + 2
in the same way that kotlin would allow abbreviating it as
{ it + 2 }
with its equivalent default lambda parameter name, "it".
In the example you quoted, the identifier denotes the sole parameter, so in a sense its name does not matter, and as such people from certain programming circles might be inclined to use an underscore instead of taking the time to come up with an appropriate name. It's not like a more descriptive name would help in that example, the type name and function name already give sufficient context for it to be perfectly clear what the parameter is for, and it's not like parameter names have any semantic significance in C.
- jiive 6y ago> It's not like a more descriptive name would help in that example, the type name and function name already give sufficient context for it to be perfectly clear what the parameter is for, and it's not like parameter names have any semantic significance in C. I agree, and I’ve been thinking about this today. There is also a minimalistic quality to this style, the _ pointer is more prominent simply by not having a real name. I like it!