elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.65k stars 299 forks source link

try / else docs #1782

Closed Duncan3142 closed 6 months ago

Duncan3142 commented 6 months ago

Clarify code examples regarding required catch or finally when using else

Fixes #1734

Duncan3142 commented 6 months ago

As an observation, the syntax for try/else/finally is a bit confusing as a new user.

The syntax implies the else happens instead of the try

Also, is there a need for such a construct?

For example, instead of

try {
  cat log-file > /tmp/log-snapshot
} else {
  aws s3api put-object --bucket logs --key logs/latest --body /tmp/log-snapshot
} finally { 
  rm -f /tmp/log-snapshot
}

would it not be sufficient to simply do

try {
  cat log-file > /tmp/log-snapshot
  aws s3api put-object --bucket logs --key logs/latest --body /tmp/log-snapshot
} finally { 
  rm -f /tmp/log-snapshot
}

If there is a valid use case for try/else/finally I'd be happy to add it to this PR

xiaq commented 6 months ago

As an observation, the syntax for try/else/finally is a bit confusing as a new user.

The syntax implies the else happens instead of the try

Also, is there a need for such a construct?

For example, instead of

try {
  cat log-file > /tmp/log-snapshot
} else {
  aws s3api put-object --bucket logs --key logs/latest --body /tmp/log-snapshot
} finally { 
  rm -f /tmp/log-snapshot
}

would it not be sufficient to simply do

try {
  cat log-file > /tmp/log-snapshot
  aws s3api put-object --bucket logs --key logs/latest --body /tmp/log-snapshot
} finally { 
  rm -f /tmp/log-snapshot
}

If there is a valid use case for try/else/finally I'd be happy to add it to this PR

Right, there is no reason to use try/else/finally, the else clause is there to pair with the catch.

Took a look at Python's try syntax and it does require else to come after a catch clause, so I'll also make that a requirement too.

Duncan3142 commented 6 months ago

As an observation, the syntax for try/else/finally is a bit confusing as a new user. The syntax implies the else happens instead of the try Also, is there a need for such a construct? For example, instead of

try {
  cat log-file > /tmp/log-snapshot
} else {
  aws s3api put-object --bucket logs --key logs/latest --body /tmp/log-snapshot
} finally { 
  rm -f /tmp/log-snapshot
}

would it not be sufficient to simply do

try {
  cat log-file > /tmp/log-snapshot
  aws s3api put-object --bucket logs --key logs/latest --body /tmp/log-snapshot
} finally { 
  rm -f /tmp/log-snapshot
}

If there is a valid use case for try/else/finally I'd be happy to add it to this PR

Right, there is no reason to use try/else/finally, the else clause is there to pair with the catch.

Took a look at Python's try syntax and it does require else to come after a catch clause, so I'll also make that a requirement too.

Thanks for clarifying