untoldwind / KontrolSystem2

Autopilot scripting system for KSP2
Other
54 stars 14 forks source link

Different result types? #177

Open PhilouDS opened 1 month ago

PhilouDS commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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.