views:

139

answers:

6

c, java and many other languages do not pay attention to return values.

int   i = func()
float f = func()
int   func() { return 5 }
float func() { return 1.3}

Why isnt the above legal? Does it make it more difficult to program

int i = func(func(func(func2(func3())))) //you dont know what you are getting

Is it hard to write a compiler? are there more language unambiguity? Is there a language that can do the above?

+10  A: 

Let's say it was allowed, which one would this call:

func();

That may not the the full answer, but I believe that is one reason why it is not legal.

Fredrik Mörk
that's a good point. Parameters are generally required for functions, but handling the return value isn't.
Herms
I think it is bad practice to not handle return values. This should be void throwning an exception or the return value should be checked. However var v = func(); (C# style assignment) may cause problems... (hmmmm))
acidzombie24
+1 for making me admit the var v= above
acidzombie24
I'm not sure I agree with you on the "bad practice" part, at least not in the general case. It's certainly not something that should be explicitly forbidden, so the language would have to be able to handle this.
Herms
A good point, but rules could also be added to set a default implementation of Func(), either explicitly or by some set of rules.
CaptnCraig
+8  A: 

What about this case?

class A implements Foo { /*...*/ }
class B implements Foo { /*...*/ }

A func() { return new A(); }
B func() { return new B(); }

Foo v = func(); // which do you call?!

There are already problems with ambiguity when you allow overloading of a single function name that take different arguments. Having to check the return type as well would probably make resolving the right function a lot harder.

I'm sure a language could implement that, but it would make things a lot more complicated, and would generally make the code harder to understand.

Herms
Similarly what should `System.out.println(func() + func())` output?
notnoop
That would make code harder to understand. +1. Maybe i'll mark this as correct (i want to see other answers)
acidzombie24
+4  A: 

For a C++ example, consider:

void g(int x);
void g(float x);
g(func());

Which of the overloaded g() functions would be called?

Greg Hewgill
in this case it could do what C++ does at times and compiles with an ambiguous error forcing the user to write `g((Float_OR_int_type)func());`
acidzombie24
Even that wouldn't help. What if you have multiple methods that return a type that can be validly cast to the object you're casting to? int can be cast to float, and vice-versa.
Herms
A: 

Most languages allow for mixed-mode operations with automatic coercion (e.g. float + int), where multiple interpretations are legal. Without coercion, working with multiple numeric types (short, int, long, float, double) would become very cumbersome; with coercion, return-type based disambigation would lead to hard-to-understand code.

mfx
+1  A: 

Allowing these may introduce problems. For example:

int i = func2(func());
int func() { return 5 }
float func() { return 1.3 }
int func(float a) { return a }
int func(int a) { return a }

This is ambiguous.

egon
+2  A: 

Perl allows a certain degree of return type variation, in that functions can tell what kind of context they're being evaluated in. For instance, a function that might return an array can see that it's running in a scalar context, and just return the putative length directly, sparing the allocation and initialization of the array just to get its length.

Novelocrat
Which works for Perl, but there are several things in Perl that I don't think would work in a more conventional language. Not to mention that Perl doesn't have a type system in the normal sense: types are $scalars, @arrays, #hashes, @typeglobs, and it seems to me I've forgotten one. There is no distinction between integer, float, and string, and operators have to figure out whether to act numerically ($i + 1) or with strings ($i + "1").
David Thornley
@David: I don't think I was praising any virtues of Perl in my response, nor suggesting that such things *should* be added to other languages. I was merely answering the part of the question that asks what's out there.
Novelocrat