EDIT: As several respondents have noted in the answers and comments below, the original example had a default value that would never be used because of the way patterns and default values are applied. I've edited the example so that it now focuses on the question that was being asked and which has already been answered.
Is it possible to achieve the following behavior in a function definition:
Remove[foo];
foo[Optional[Pattern[x, _?IntegerQ], 1]] := x;
foo[]
foo[2]
1
2
using "colon syntax" shorthand?
Note that,
Remove[foo];
foo[x : _?IntegerQ : 1] := x;
foo[]
foo[2]
foo[]
foo[2]
does not produce the desired result.
The first code sample is too verbose; setting a default value for a function argument while simultaneously checking type when an argument is supplied should be common enough practice to deserve its own shorthand notation.
Can anyone modify the second example to achieve the desired results? If Mathematica syntax does not directly support shorthand for combining default values with argument type checking, perhaps someone could suggest how this might be achieved using the Notation package.