FAQ
Is there a way to make a function return different types but the caller
does not need to assert?

e..g

var x int = function(1);
var y float = function(2);


Assume that function returns an int or float (randomly) based on the input
1 vs. 2.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Roberto Zanotto at Jan 10, 2016 at 1:39 am
    no
    On Sunday, January 10, 2016 at 2:33:22 AM UTC+1, Lax Clarke wrote:

    Is there a way to make a function return different types but the caller
    does not need to assert?

    e..g

    var x int = function(1);
    var y float = function(2);


    Assume that function returns an int or float (randomly) based on the input
    1 vs. 2.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Tim K at Jan 10, 2016 at 7:50 am
    If you are OK with multiple return values you could do something like:
    func f(...) (int, float, Type)
    or
    func f(...) (int, float)
    if you know for example the function cannot return the zero value or
    negative numbers.
    But you still have to make a check after to see what you got back.

    Any more background on the problem you are trying to solve or is it just
    generic question?

    On Saturday, January 9, 2016 at 5:33:22 PM UTC-8, Lax Clarke wrote:

    Is there a way to make a function return different types but the caller
    does not need to assert?

    e..g

    var x int = function(1);
    var y float = function(2);


    Assume that function returns an int or float (randomly) based on the input
    1 vs. 2.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 10, '16 at 1:33a
activeJan 10, '16 at 7:50a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase