I want to be able to retrieve a value and then branch on its value, all in
one step. For some reason it irks me to have to say:
var blah = GetBlah();
if(blah != null)
{
//Do Stuff With blah
}
I want to say
if((var blah = GetBlah) != null)
{
//Do stuff with blah
}
I'm not sure why the extra line bothers me - possibly because it's a
pattern I keep having to repeat in my code, and any pattern I repeat irks
me after a while. That's why computers are there, to stop you having to do
the same thing over and over again.
one step. For some reason it irks me to have to say:
var blah = GetBlah();
if(blah != null)
{
//Do Stuff With blah
}
I want to say
if((var blah = GetBlah) != null)
{
//Do stuff with blah
}
I'm not sure why the extra line bothers me - possibly because it's a
pattern I keep having to repeat in my code, and any pattern I repeat irks
me after a while. That's why computers are there, to stop you having to do
the same thing over and over again.
no subject
Date: 2011-06-20 12:44 pm (UTC)no subject
Date: 2011-06-20 12:49 pm (UTC)no subject
Date: 2011-06-20 12:52 pm (UTC)var blah; if ((var = getBlah()) != null) { //Do stuff with blah }?
no subject
Date: 2011-06-20 12:53 pm (UTC)The closest you could get in C# would be an extension method using some generics and lambdas:
public static T DoIfNotNull<T>(this T value, Action<T> action) where T: class
{
if (value != null)
{
action(value);
}
// return the value for further chaining
return value;
}
Then you could code:
GetBlah().DoIfNotNull(blah => DoStuffWith(blah));of course there are variations on this like
public static U DoIfNotNull<T, U>(this T value, Func<T, U> func) where T : class
{
U result = default(U);
if (value != null)
{
result = func(value);
}
return result;
}
.. so then your action can change the type and you can still chain methods.
no subject
Date: 2011-06-20 01:10 pm (UTC)no subject
Date: 2011-06-20 01:12 pm (UTC)no subject
Date: 2011-06-20 01:13 pm (UTC)(blah = getBlah())returns the value assigned to blah.You'd have to declare blah without using "var" though, unless there's an assignment on the same line the type inference has nothing to work on.
so this works
string blah;
if ((blah = getBlah()) != null)
{
//Do stuff with blah
}
private string getBlah()
{ ... }
no subject
Date: 2011-06-20 01:17 pm (UTC)// code is actually shorter - method is auto cast to action
getBlah().DoIfNotNull(SomeMethod);
...
private void SomeMethod(string blah)
{
...
}
no subject
Date: 2011-06-20 01:17 pm (UTC)http://metoojava.wordpress.com/2010/11/15/java-7-awesome-features/
It's an interesting language feature I've not seen elsewhere that I hope spreads.
no subject
Date: 2011-06-20 01:34 pm (UTC)Ah yes, I'd forgotten that C# has type inference; I'd assumed that Andy's '
var' was pseudocode, heh.no subject
Date: 2011-06-20 01:43 pm (UTC)var foo = SomeExpression;and replaces "var" with the type of SomeExpressionThe stuff that OCaml (and F#) do with type inference is just amazing.
no subject
Date: 2011-06-20 02:01 pm (UTC)no subject
Date: 2011-06-20 02:02 pm (UTC)no subject
Date: 2011-06-20 02:11 pm (UTC)let f a b = a + b + 100, the type a, b and the return type of f are all infered as int.It does the equivalent of generics automatically, so
let makeTuple a b = (a, b)is the same as in C#Tuple<A, B> MakeTupple<A, B>(A a, B b) { ... }no subject
Date: 2011-06-20 05:12 pm (UTC)You can say
if (my $blah = get_blah()) {
...
}
but not
if (my $blah = get_blah() and $blah->can('do_stuff')) {
$blah->do_stuff();
}
because $blah doesn't exist when the second part of the conditional is run. You need to say
if (my $blah = get_blah()) {
if ($blah->can('do_stuff')) {
$blah->do_stuff();
}
}
instead.
no subject
Date: 2011-06-20 06:33 pm (UTC)no subject
Date: 2011-06-20 06:49 pm (UTC)It also strongly reminds me of Icon's goal-directed evaluation, but since Icon doesn't have any OO features and very very limited structures, I'm pretty sure it doesn't have anything quite like this; but if it *did*, it would support this sort of thing straight out of the box.
(I recommend giving Icon a once-over for curiosity's sake, by the way; it's an interesting little language, primarily because of the goal-oriented evaluation and generation. )
no subject
Date: 2011-06-20 07:18 pm (UTC)However, I don't think I _want_ to write what Andy wanted to write. I think the assignment and test together are two ugly. I would accept a syntax like:
or:
or possibly some other convenient shorthand.
In C++ it might be possible to make an awful preprocessor hack, something like
#define TRY(var, value) for(auto var = value;var!=null;var=null) TRY(blah, GetBlah()) { // do stuff }You mgiht be able to improve the semantics, I'm not sure. I doubt it's worth using, though. You may be able to do something fancier with templates, but it's getting it to act like an "if" statement that's hard.
no subject
Date: 2011-06-20 07:56 pm (UTC)no subject
Date: 2011-06-20 08:05 pm (UTC)no subject
Date: 2011-06-20 08:06 pm (UTC)There ought to be a good way of saying "Is there an X? If so, do stuff with it?", and I don't think I've found that just yet.
no subject
Date: 2011-06-20 08:15 pm (UTC)$saveme = $boring or $interesting;
$boring gets skipped if it gets evaluated as false in some way or other.
Apparently PHP now has this as:
$saveme = $boring ?: $interesting;
which... urgh.
no subject
Date: 2011-06-20 08:36 pm (UTC)no subject
Date: 2011-06-20 08:46 pm (UTC)if (blahType blah = GetBlah()) { ... }no subject
Date: 2011-06-20 08:53 pm (UTC)One of my favourites over the years has been something along the lines of:
my $result = $cache-<{$key} ||= (...);...which assigns to $result using a cache of result values: if $result->{$key} exists and evaluates true (as strings do), it just assigns from the hashref dereference. If the hash doesn't contain a value for $key, the (...) code is evaluated, its result assigned to $cache->{$key} and to $result.