Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different result types? #177

Open
PhilouDS opened this issue May 24, 2024 · 3 comments
Open

Different result types? #177

PhilouDS opened this issue May 24, 2024 · 3 comments

Comments

@PhilouDS
Copy link
Contributor

Hi,

As I mentioned in issue #151, I'm building an UI where the player can choose between selecting a vessel or enter geocoordinates.

This UI is called by a function (let call it select_fn). In the first case, the result has type Vessel and, in the second case, the result has type GeoCoordinates.

Is it even possible to automatically change the type of a function's result?

For now, I have a solution. My select_fn function returns a triple (string, float, float). If the first string is the name of a body, then I know than the 2 other floats are latitude and longitude. In this case, I create a variable of type Geocoordinates.
Else, the string result should be the name of a vessel and the two floats are its geocoordinates. In this case, I can create a variable of type Vessel.

Do you think of an other way to do that?

@lefouvert
Copy link

At this time, as far as I know, No.
Maybe if this language support fn foobar(param0: T, param1: int) -> T ={} but i'm not even sure it would solve your issue.
The other way is not supported by the language as it is heavily typed, which is good.

@untoldwind
Copy link
Owner

A slightly better variant might be to use options:

fn selector() -> (Option<Vessel>, Option<GeoCoordinates>) = ...

An then return either (Some(vessel), None) or (None, Some(geo_coordinates)).

Or maybe even wrap it in a struct with some helper-method .is_vessel(), .is_geo_coordinate() or so.

@PhilouDS
Copy link
Contributor Author

Thank you, I'll try the two options :)

A slightly better variant might be to use options:

fn selector() -> (Option<Vessel>, Option<GeoCoordinates>) = ...

An then return either (Some(vessel), None) or (None, Some(geo_coordinates)).

Or maybe even wrap it in a struct with some helper-method .is_vessel(), .is_geo_coordinate() or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants