Questions tagged [return-type]
This tag is for questions dealing with the type of the variable or value returned by a function.
return-type
1,541
questions
844
votes
19
answers
460k
views
What should main() return in C and C++?
What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments?
If int main() then return 1 or return 0?
683
votes
4
answers
422k
views
How to specify multiple return types using type-hints
I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints?
For example, is this the correct way to do it?
def foo(id) -> list ...
375
votes
17
answers
354k
views
Apply pandas function to column to create multiple new columns?
How to do this in pandas:
I have a function extract_text_features on a single text column, returning multiple output columns. Specifically, the function returns 6 values.
The function works, however ...
220
votes
7
answers
415k
views
How do I make the return type of a method generic?
Is there a way to make this method generic so I can return a string, bool, int, or double? Right now, it's returning a string, but if it's able find "true" or "false" as the ...
204
votes
3
answers
121k
views
Nullable return types in PHP7
PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for ...
189
votes
1
answer
202k
views
C++: variable 'std::ifstream ifs' has initializer but incomplete type
Sorry if this is pretty noobish, but I'm pretty new to C++. I'm trying to open a file and read it using ifstream:
vector<string> load_f(string file) {
vector<string> text;
ifstream ...
146
votes
4
answers
46k
views
What's the difference between returning void and returning a Task?
In looking at various C# Async CTP samples I see some async functions that return void, and others that return the non-generic Task. I can see why returning a Task<MyType> is useful to return ...
146
votes
4
answers
20k
views
Why doesn't C# support the return of references?
I have read that .NET supports return of references, but C# doesn't. Is there a special reason? Why I can't do something like:
static ref int Max(ref int x, ref int y)
{
if (x > y)
return ...
129
votes
16
answers
156k
views
Returning anonymous type in C#
I have a query that returns an anonymous type and the query is in a method. How do you write this:
public "TheAnonymousType" TheMethod(SomeParameter)
{
using (MyDC TheDC = new MyDC())
{
var ...
129
votes
5
answers
224k
views
Return from lambda forEach() in java
I am trying to change some for-each loops to lambda forEach()-methods to discover the possibilities of lambda expressions. The following seems to be possible:
ArrayList<Player> playersOfTeam = ...
124
votes
4
answers
36k
views
Why can't 'kotlin.Result' be used as a return type?
I've created a method, and the return is Result<R> in a class of MyClass<R>, but the error message is: 'kotlin.Result' cannot be used as a return type
I've also looked into the Result ...
100
votes
6
answers
78k
views
Java generics void/Void types
I am implementing a ResponseHandler for the apache HttpClient package, like so:
new ResponseHandler<int>() {
public int handleResponse(...) {
// ... code ...
return 0;
}
...
100
votes
4
answers
65k
views
PHP 7 interfaces, return type hinting and self
UPDATE: PHP 7.4 now does support covariance and contravariance which addresses the major issue raised in this question.
I have run into something of an issue with using return type hinting in PHP 7....
98
votes
8
answers
129k
views
C# numeric enum value as string
I have the following enum:
public enum Urgency {
VeryHigh = 1,
High = 2,
Routine = 4
}
I can fetch an enum "value" as string like this:
((int)Urgency.Routine).ToString() // returns ...
98
votes
9
answers
159k
views
Can two Java methods have same name with different return types? [duplicate]
Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name.
Is that allowed?
93
votes
3
answers
67k
views
C++ virtual function return type
Is it possible for an inherited class to implement a virtual function with a different return type (not using a template as return)?
88
votes
12
answers
81k
views
Is it safe to return a struct in C or C++?
What I understand is that this shouldn't be done, but I believe I've seen examples that do something like this (note code is not necessarily syntactically correct but the idea is there)
typedef ...
74
votes
4
answers
4k
views
Why does Enumeration get converted to ArrayList and not List in java.utils?
Is there a good reason that the Collections.list() method in the java.utils package returns an ArrayList<T> instead of List<T>?
Obviously an ArrayList is a List, but I'm under the ...
70
votes
4
answers
77k
views
Return CompletableFuture<Void> or CompletableFuture<?>?
I want to write an asynchronous method that returns a CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return ...
65
votes
5
answers
85k
views
Void as return type
I was testing return types with PHP 7.
I've created a simple script to test return types of PHP 7:
<?php
Class Obj {
public function __construct(){
}
public function test(): ...
65
votes
3
answers
91k
views
What if I write return statement in constructor?
What if I write return statement in constructor? Is it standard conformant?
struct A
{
A() { return; }
};
The above code compiles fine, without any error at ideone. But the following code ...
57
votes
4
answers
12k
views
Simplest way to determine return type of function
Given a very simple, but lengthy function, such as:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
What is the most simple and concise way to determine the ...
57
votes
14
answers
9k
views
Why can't I explicitly return void from a method?
void run() {
...
if (done) return cancel();
...
}
where cancel() return void. This won't compile... and I can almost understand why. But if I want to return a void from a void, why not? ...
57
votes
3
answers
41k
views
Should I use Unit or leave out the return type for my scala method?
I am not sure what the difference is between specifying Unit as the return type of my scala method or leaving out the return type altogether. What is the difference?
Can anyone please advise?
56
votes
7
answers
82k
views
Return multiple values from a Java method: why no n-tuple objects?
Why isn't there a (standard, Java certified) solution, as part of the Java language itself, to return multiple values from a Java method, rather than developers having to use their own means, such as ...
50
votes
1
answer
712
views
Unexpected implicit resolution based on inference from return type
Given a typeclass where instance selection should be performed based on the return type:
case class Monoid[A](m0: A) // We only care about the zero here
implicit def s[T] : Monoid[Set[T]] = Monoid(...
46
votes
1
answer
70k
views
How to specify method return type list of (what) in Python?
Let's say I have a method like the following
def validate(self, item:dict, attrs:dict)-> list:
If I want to be more specific and tell that my return type is a list of ValidationMessages?
How ...
45
votes
3
answers
61k
views
Why do constructors in java not have a return type? [duplicate]
Possible Duplicate:
Why constructor not returns value
Why don't constructors have a return type, not even void? What's the reason for that?
44
votes
5
answers
63k
views
Can a C# method return a method?
Can a method in C# return a method?
A method could return a lambda expression for example, but I don't know what kind of type parameter could I give to such a method, because a method isn't Type. ...
44
votes
14
answers
190k
views
Return different type of data from a method in java?
public static void main(String args[]) {
myMethod(); // i am calling static method from main()
}
.
public static ? myMethod(){ // ? = what should be the return type
return value;// is ...
42
votes
10
answers
6k
views
Is there a standard "never returns" attribute for C# functions?
I have one method that looks like this:
void throwException(string msg)
{
throw new MyException(msg);
}
Now if I write
int foo(int x, y)
{
if (y == 0)
throwException("Doh!");
...
40
votes
7
answers
33k
views
Why should functions always return the same type?
I read somewhere that functions should always return only one type
so the following code is considered as bad code:
def x(foo):
if 'bar' in foo:
return (foo, 'bar')
return None
I guess the ...
40
votes
7
answers
16k
views
How can I return an anonymous struct in C?
Trying some code, I realized that the following code compiles:
struct { int x, y; } foo(void) {
}
It seems as if we are defining a function named foo which returns an anonymous struct.
Does it only ...
40
votes
2
answers
23k
views
SQL function return-type: TABLE vs SETOF records
What's the difference between a function that returns TABLE vs SETOF records, all else equal.
CREATE FUNCTION events_by_type_1(text) RETURNS TABLE(id bigint, name text) AS $$
SELECT id, name FROM ...
37
votes
4
answers
9k
views
Return Type Covariance with Smart Pointers
In C++ we can do this:
struct Base
{
virtual Base* Clone() const { ... }
virtual ~Base(){}
};
struct Derived : Base
{
virtual Derived* Clone() const {...} //overrides Base::Clone
};
...
36
votes
6
answers
19k
views
When is C++ covariance the best solution?
This question was asked here a few hours ago and made me realise that
I have never actually used covariant return types in my own code. For those
not sure what covariance is, it's allowing the return ...
34
votes
12
answers
50k
views
PHP class method return type
Is it possible to define the return type like this?
public static function bool Test($value)
{
return $value; //this value will be bool
}
32
votes
3
answers
2k
views
Can we have a function with multiple return types? (in C++11 and above)
I am trying to write a generic function which takes input as uint8, uint16, uint32, uint64, .... and return the maximum value with the datatype of largest element?
For example:
template < ...
31
votes
6
answers
16k
views
How to get the return type of a method call in IntelliJ?
Normally you would expect just hovering over a method it would show a popup of the return type.
How do you get this information in intellij ultimate?
30
votes
2
answers
46k
views
What does Void return type mean in Kotlin
I tried to create function without returning value in Kotlin. And I wrote a function like in Java but with Kotlin syntax
fun hello(name: String): Void {
println("Hello $name");
}
And I've got an ...
30
votes
1
answer
19k
views
Return None from python function annotated with mypy, multiple return types
I come from a Typescript background. I'm bringing static type checking into a python project I'm working on (using mypy).
In Typescript, it is valid to return null from a function that is annotated ...
30
votes
7
answers
14k
views
Why does Array.prototype.push return the new length instead of something more useful?
Ever since its introduction in ECMA-262, 3rd Edition, the Array.prototype.push method's return value is a Number:
15.4.4.7 Array.prototype.push ( [ item1 [ , item2 [ , … ] ] ] )
The arguments ...
28
votes
4
answers
15k
views
Why should the assignment operator return a reference to the object?
I'm doing some revision of my C++, and I'm dealing with operator overloading at the minute, specifically the "="(assignment) operator. I was looking online and came across multiple topics discussing ...
27
votes
2
answers
22k
views
ActiveRecord 'destroy' method returns a boolean value in Ruby on Rails?
I am using Ruby on Rails 3 and I would like to know what type of return will have the following code:
@user.destroy
I need that to handle cases on success and fault in someway like this:
if @user....
24
votes
7
answers
65k
views
Dynamic Return Type in Java method
I've seen a question similar to this multiple times here, but there is one big difference.
In the other questions, the return type is to be determined by the parameter. What I want/need to do is ...
24
votes
12
answers
60k
views
How to return local array in C++?
char *recvmsg(){
char buffer[1024];
return buffer;
}
int main(){
char *reply = recvmsg();
.....
}
I get a warning:
warning C4172: returning address of local variable or temporary
24
votes
4
answers
26k
views
php7 void return type not working?
I have a problem with return types in php7, specially "void".
it works with all other types, int, string, null, bool, class objects.
but when i use void it expecting me to return an instance of ...
23
votes
4
answers
28k
views
Returning an objects subclass with generics
With an abstract class I want to define a method that returns "this" for the subclasses:
public abstract class Foo {
...
public <T extends Foo> T eat(String eatCake) {
....
22
votes
12
answers
4k
views
Why does Java Specifications want the main method of a program to be void only? [duplicate]
Although the return type is not a part of a method's signature, JVM looks for exact declaration as
public static void main(String[] args)
My assumption is that since method signature does not have "...
20
votes
8
answers
56k
views
return empty List in catch block
I have a c# function that reads file locations from a Datatable, and returns a List with all the file lcoations to the calling method.
In the Catch block, I want to return an empty list with a false ...