Forums

Shortcut syntax idea for catch

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Shortcut syntax idea for catch

Postby Charles » Sat Nov 15, 2008 7:01 pm

Motivations:
-- Often you want to catch an exception for just one line of code.
-- Wrapping a line in try...catch tends to make the code look cruftier as the line in questions gets indented and wrapped.

Idea:
class A

def main is shared

# normal, full syntax:

try
x = int.parse('5')
catch FormatError
x = 0

# short cut syntax for catching exceptions from a single statement:

x = int.parse('5')
catch FormatError, x = 0

So you can just slap an indented "catch" underneath the line of code to handle whatever errors you are expecting. The line of code remains prominent.

This is not implemented. It's just an idea I'm throwing out here.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Shortcut syntax idea for catch

Postby helium » Sun Nov 23, 2008 6:05 am

SML has this. There catch is called handle. It's more declarative than what you suggest here

It would look something like this
Code: Select all
x = int.parse('5') handle FormatError => 0


So the expression "int.parse('5') handle FormatError => 0" is 0 if a FormatError exception is thrown.
helium
 
Posts: 14

Re: Shortcut syntax idea for catch

Postby Charles » Mon Nov 24, 2008 12:00 pm

I like it as it matches up with an aspect of Cobra whereby statements can have expression based equivalents. That includes "for" and "if", for example.

Having the statement version of a "hanging catch" could still be a legit separate feature, but we could start with the expression-based catch first to see if the "hanging catch" would still be desireable.

I like to reuse the same keywords in the expression-based form of a statement, and typically use the same order that the statements would use, so it would be:
x = int.parse(input) catch FormatError get 0

The use of "get" comes from the for expression syntax.

ticket:86
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Shortcut syntax idea for catch

Postby gauthier » Mon Nov 24, 2008 6:12 pm

It's unclear to me if it's some kind of ternary affectation working on exception instead of bool or some wider control structure

will this work for plain statements:
problemEncountered = false
openConnection() catch Exception problemEncountered = true

I'm also wondering if prefixing with on will make sense (or not)?
on openConnection catch Exception pass

also what's about considering rescue keyword?
openConnection rescue Exception pass
gauthier
 
Posts: 116

Re: Shortcut syntax idea for catch

Postby agustech » Tue Nov 25, 2008 2:23 am

I see it mostly unnecessary, we only save 'try'. Also, it may confuse people trying to find the missing 'try' too.

I like python idea of 'There should be one-- and preferably only one --obvious way to do it.'

Code: Select all
# this?

try
  x = int.parse('5')
catch FormatError, x = 0

#instead of this?:

x = int.parse('5')
  catch FormatError, x = 0

agustech
 
Posts: 37

Re: Shortcut syntax idea for catch

Postby Charles » Tue Nov 25, 2008 2:57 am

gauthier wrote:It's unclear to me if it's some kind of ternary affectation working on exception instead of bool or some wider control structure

will this work for plain statements:
problemEncountered = false
openConnection() catch Exception problemEncountered = true

I'm also wondering if prefixing with on will make sense (or not)?
on openConnection catch Exception pass

also what's about considering rescue keyword?
openConnection rescue Exception pass

The expression form, if implemented, would be:
<expr-a> catch <exception> get <expr-b>

It operates on any <expr-a> that has a non-void value.

The statement form, if implemented, would be:
<statement-a>
catch <exception>
<statement-b>

Or:
<statement-a>
catch <exception>, <statement-b>

Note that you can already do the following:
try, <statement-a>
catch <exception>, <statement-b>

And then I speculated that perhaps the expression form was all that was needed. If we prefixed the expression form, the word "try" would make the most sense:
i = try int.parse(input) catch FormatError get 0

Both "if" and "for" expressions start with a keyword that matches their statement-based cousin, so this might be the way to go.

And likewise, since we already have "catch" I think it makes more sense than "rescue".

I hope that clarifies.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Shortcut syntax idea for catch

Postby Charles » Tue Nov 25, 2008 3:12 am

agustech wrote:I see it mostly unnecessary, we only save 'try'. Also, it may confuse people trying to find the missing 'try' too.

I like python idea of 'There should be one-- and preferably only one --obvious way to do it.'

Code: Select all
# this?

try
  x = int.parse('5')
catch FormatError, x = 0

#instead of this?:

x = int.parse('5')
  catch FormatError, x = 0


I see what you're saying about the missing "try" but then that was part of the point: to let the main statement appear at the same level as other statements as something normal and relegate the exception handling to something that was out of the way.

Re: Python's "only one way" myth:

Do you use print (whether statement or function) or file.write()?

Do you use map(), filter(), et al, or list comprehensions?

Does your module have a series of functions that operate on composite data, such as a dictionary or tuple? Or do you use classes and objects?

Do you prefer list comprehensions or "for" loop statements?

I think it's hard to make a language expressive that also only provides one obvious way to do something. For each of us, some of the questions above have an "obvious answer", but then it may vary between developers!

Getting back to Cobra, the rule of thumb is to use the most succinct syntax that you're comfortable with. For example:
# full syntax:
pro two as int
get
return 2

# succinct syntax:
get two as int
return 2
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Shortcut syntax idea for catch

Postby agustech » Tue Nov 25, 2008 5:00 am

I see your points.

Having to choose one option I would choose:

Code: Select all
i = try int.parse(input) catch FormatError get 0
agustech
 
Posts: 37

Re: Shortcut syntax idea for catch

Postby Charles » Tue Nov 25, 2008 5:14 am

I agree.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Shortcut syntax idea for catch

Postby helium » Tue Nov 25, 2008 6:58 am

I realy like that one, too.
helium
 
Posts: 14

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 33 guests