3 ms·
For JS, you can do function length() { const { x, y, z } = this return math.sqrt(x * x + y * y + z * z) } I think it's pretty short, reada
by methyl 10y ago
For JS, you can do
function length() {
const { x, y, z } = this
return math.sqrt(x * x + y * y + z * z)
}
I think it's pretty short, readable and explicit.
EDIT: formatting
- fnl 10y agoAnd make every one-liner a two-liner?
- Tarean 10y agoIn rust you can use pattern matching in arguments: struct Foo { x: f64, y: f64, z: f64 } fn length(Foo {x, y, z}: Foo) -> f64 { (x*x + y*y + z*z).sqrt() } ...Doesn't work with `self` because that would be implicit again, though. Anyway, I think that it is a minor inconvenience that isn't important for oneliners and extremely helpful when reading larger functions.
- masklinn 10y ago> Doesn't work with `self` because that would be implicit again, though. AFAIK it doesn't work on self because the [&[mut ]]self parameter is more or less a keyword determining ownership interaction with the call subject. The UFCS RFC would have made it sugar for `self: [&[mut ]]Self` (IIRC) but I believe that floundered.
- drabiega 10y agoIt actually uses less characters, since you've eliminated all the repeated uses of 'this.'. As a general rule I'm in favor of a turning a one liner into a two liner if it improves readability, especially when it reduces actual typing.