UberService().with(|UberService s| {
// some code
})
is apparently not a valid statement. And this is why (compiler::CallExpr.isStmt):
// with block applied to stand alone constructor is not valid stmt
if (method.name == "with" && target is CallExpr && ((CallExpr)target).method.isCtor)
return false
First of all, Sth() { ... } can make perfect sense as a useful statement even if it-block is passed to constructor.
Anyway, I think that explicit call to with shouldn't be any different from any other explicit call.
brianTue 15 Feb 2011
Any construction of an object not assigned to assigned to a local variable or passed to a parameter isn't considered a statement. Try assigning it to a local variable and it should work.
vkuzkokovTue 15 Feb 2011
It's not just construction. It's a method call on a newly constructed object (which is allowed both according to docs and current behavior). But
Sth().doWeirdStuff { ... }
is allowed (if we have appropriate method signature) while
Sth().with { ... }
is not.
brianTue 15 Feb 2011
True it is method call, but we treat that with-block (either explicit or implicit) as part of the construction process. It isn't a perfect rule, but I've found it often is a very good check for catching bugs.
vkuzkokov Tue 15 Feb 2011
So this:
is apparently not a valid statement. And this is why (
compiler::CallExpr.isStmt
):First of all,
Sth() { ... }
can make perfect sense as a useful statement even if it-block is passed to constructor.Anyway, I think that explicit call to
with
shouldn't be any different from any other explicit call.brian Tue 15 Feb 2011
Any construction of an object not assigned to assigned to a local variable or passed to a parameter isn't considered a statement. Try assigning it to a local variable and it should work.
vkuzkokov Tue 15 Feb 2011
It's not just construction. It's a method call on a newly constructed object (which is allowed both according to docs and current behavior). But
is allowed (if we have appropriate method signature) while
is not.
brian Tue 15 Feb 2011
True it is method call, but we treat that with-block (either explicit or implicit) as part of the construction process. It isn't a perfect rule, but I've found it often is a very good check for catching bugs.