OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Specifying type hint using the return type of a function

  • Thread starter Thread starter Altan
  • Start date Start date
A

Altan

Guest
I want to hint the type of a variable as the return type of a specific function, (without having to manually specify what the return type of the function is).

I am unable to extract the return value type from the function in a way that it can be used as a hint for another variable.

Code:
def dostuff() -> T:
    StuffContainer = namedtuple("StuffContainer", ["x", "y"])
    return StuffContainer(1, 2)

def getX(a : T):
    return a.x

The C++ equivalent of what I want done:

Code:
auto dostuff() {
    struct data {
        int x;
        int y;
    };
    return data {1, 2};
}

int getX(decltype(dostuff()) a) {
    return a.x;
}
<p>I want to hint the type of a variable as the return type of a specific function, (without having to manually specify what the return type of the function is).</p>

<p>I am unable to extract the return value type from the function in a way that it can be used as a hint for another variable.</p>

<pre class="lang-py prettyprint-override"><code>def dostuff() -> T:
StuffContainer = namedtuple("StuffContainer", ["x", "y"])
return StuffContainer(1, 2)

def getX(a : T):
return a.x
</code></pre>

<p>The C++ equivalent of what I want done:</p>

<pre class="lang-cpp prettyprint-override"><code>auto dostuff() {
struct data {
int x;
int y;
};
return data {1, 2};
}

int getX(decltype(dostuff()) a) {
return a.x;
}
</code></pre>
 
Top