compare two values of different type (but identical underlying type)Compare two dates with JavaScriptCompare two structs' values in C#What are the differences between type() and isinstance()?Comparing Class Types in JavaCompare two files in Visual StudioWhat is the “underlying type” in Scala?Function calls, assignment, and 'underlying types' in GoJava: comparing two Objects of different type with same value, returning truecompare two columns value in dataframeGolang's Bool Type

Why can't you reverse the order of the input redirection operator for while loops?

How should i charge 3 lithium ion batteries?

Was the Boeing 2707 design flawed?

Beginner to guitar playing - where should I begin?

Can an Arcane Focus be embedded in one's body?

How does the OS tell whether an "Address is already in use"?

Why is a statement like 1 + n *= 3 allowed in Ruby?

What are the occurences of total war in the Native Americans?

Is the internet in Madagascar faster than in UK?

What is the meaning of “these lederhosen are riding up my Bundesliga”?

Why error propagation in CBC mode encryption affect two blocks?

rationalizing sieges in a modern/near-future setting

Under what circumstances does intermodulation become an issue in a filter after LNA setup?

1mth baby boy keeps peeing through diapers, sometimes diper seems practically unused

about to retire but not retired yet, employed but not working any more

Should I stick with American terminology in my English set young adult book?

Why is getting a PhD considered "financially irresponsible"?

How do I remap "å" to type "å"?

Where does learning new skills fit into Agile?

Thought experiment and possible contradiction between electromagnetism and special relativity

Why does a sticker slowly peel off, but if it is pulled quickly it tears?

Joining lists with same elements

How do you capitalize agile costs with less mature teams?

Unlock your Lock



compare two values of different type (but identical underlying type)


Compare two dates with JavaScriptCompare two structs' values in C#What are the differences between type() and isinstance()?Comparing Class Types in JavaCompare two files in Visual StudioWhat is the “underlying type” in Scala?Function calls, assignment, and 'underlying types' in GoJava: comparing two Objects of different type with same value, returning truecompare two columns value in dataframeGolang's Bool Type






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















How do I compare the values of two variables that have different types but the same underlying type.



type SomeID uint8
type OtherID uint8

func areEqual(sid SomeID, oid OtherID) bool
// error: mismatched types
return sid == oid










share|improve this question



















  • 4





    First convert them back to uint8 and then compare those uints.

    – mkopriva
    Mar 27 at 20:02







  • 4





    Though this does raise the question of why these are different types; the point of creating primitive-based types is usually to distinguish them, meaning that comparing a SomeID to a OtherID shouldn't yield any meaningful result; like comparing a time.Month to a time.Weekday, which are both int but have different meaning and are therefore incomparable.

    – Adrian
    Mar 27 at 20:27






  • 1





    exactly - and usually one adds methods to a distinct type which ultimately define its unique characteristics.

    – colminator
    Mar 27 at 20:29











  • @Adrian I agree, however this was part of an assignment so the problem is rather fabricated.

    – octavio
    Mar 28 at 11:03

















0















How do I compare the values of two variables that have different types but the same underlying type.



type SomeID uint8
type OtherID uint8

func areEqual(sid SomeID, oid OtherID) bool
// error: mismatched types
return sid == oid










share|improve this question



















  • 4





    First convert them back to uint8 and then compare those uints.

    – mkopriva
    Mar 27 at 20:02







  • 4





    Though this does raise the question of why these are different types; the point of creating primitive-based types is usually to distinguish them, meaning that comparing a SomeID to a OtherID shouldn't yield any meaningful result; like comparing a time.Month to a time.Weekday, which are both int but have different meaning and are therefore incomparable.

    – Adrian
    Mar 27 at 20:27






  • 1





    exactly - and usually one adds methods to a distinct type which ultimately define its unique characteristics.

    – colminator
    Mar 27 at 20:29











  • @Adrian I agree, however this was part of an assignment so the problem is rather fabricated.

    – octavio
    Mar 28 at 11:03













0












0








0








How do I compare the values of two variables that have different types but the same underlying type.



type SomeID uint8
type OtherID uint8

func areEqual(sid SomeID, oid OtherID) bool
// error: mismatched types
return sid == oid










share|improve this question














How do I compare the values of two variables that have different types but the same underlying type.



type SomeID uint8
type OtherID uint8

func areEqual(sid SomeID, oid OtherID) bool
// error: mismatched types
return sid == oid







go types compare






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 20:00









octaviooctavio

1848 bronze badges




1848 bronze badges










  • 4





    First convert them back to uint8 and then compare those uints.

    – mkopriva
    Mar 27 at 20:02







  • 4





    Though this does raise the question of why these are different types; the point of creating primitive-based types is usually to distinguish them, meaning that comparing a SomeID to a OtherID shouldn't yield any meaningful result; like comparing a time.Month to a time.Weekday, which are both int but have different meaning and are therefore incomparable.

    – Adrian
    Mar 27 at 20:27






  • 1





    exactly - and usually one adds methods to a distinct type which ultimately define its unique characteristics.

    – colminator
    Mar 27 at 20:29











  • @Adrian I agree, however this was part of an assignment so the problem is rather fabricated.

    – octavio
    Mar 28 at 11:03












  • 4





    First convert them back to uint8 and then compare those uints.

    – mkopriva
    Mar 27 at 20:02







  • 4





    Though this does raise the question of why these are different types; the point of creating primitive-based types is usually to distinguish them, meaning that comparing a SomeID to a OtherID shouldn't yield any meaningful result; like comparing a time.Month to a time.Weekday, which are both int but have different meaning and are therefore incomparable.

    – Adrian
    Mar 27 at 20:27






  • 1





    exactly - and usually one adds methods to a distinct type which ultimately define its unique characteristics.

    – colminator
    Mar 27 at 20:29











  • @Adrian I agree, however this was part of an assignment so the problem is rather fabricated.

    – octavio
    Mar 28 at 11:03







4




4





First convert them back to uint8 and then compare those uints.

– mkopriva
Mar 27 at 20:02






First convert them back to uint8 and then compare those uints.

– mkopriva
Mar 27 at 20:02





4




4





Though this does raise the question of why these are different types; the point of creating primitive-based types is usually to distinguish them, meaning that comparing a SomeID to a OtherID shouldn't yield any meaningful result; like comparing a time.Month to a time.Weekday, which are both int but have different meaning and are therefore incomparable.

– Adrian
Mar 27 at 20:27





Though this does raise the question of why these are different types; the point of creating primitive-based types is usually to distinguish them, meaning that comparing a SomeID to a OtherID shouldn't yield any meaningful result; like comparing a time.Month to a time.Weekday, which are both int but have different meaning and are therefore incomparable.

– Adrian
Mar 27 at 20:27




1




1





exactly - and usually one adds methods to a distinct type which ultimately define its unique characteristics.

– colminator
Mar 27 at 20:29





exactly - and usually one adds methods to a distinct type which ultimately define its unique characteristics.

– colminator
Mar 27 at 20:29













@Adrian I agree, however this was part of an assignment so the problem is rather fabricated.

– octavio
Mar 28 at 11:03





@Adrian I agree, however this was part of an assignment so the problem is rather fabricated.

– octavio
Mar 28 at 11:03












2 Answers
2






active

oldest

votes


















3















As mentioned by mkopriva, type-conversion is needed to compare the values.



https://tour.golang.org/basics/13



func areEqual(sid SomeID, oid OtherID) bool 
return uint8(sid) == uint8(oid)






share|improve this answer
































    0















    You can be less accurate, you don't need to remember underlying type



    func areEqual(sid SomeID, oid OtherID) bool 
    return sid == SomeID(oid)



    And you can be more generic



    func areEqual(x, y interface) (bool, error) 
    xv := reflect.ValueOf(x)
    yv := reflect.ValueOf(y)
    if yv.Type().ConvertibleTo(xv.Type())
    return xv.Interface() == yv.Convert(xv.Type()).Interface(), nil
    else
    return false, errors.New("Types are mismatched")




    Playground






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385539%2fcompare-two-values-of-different-type-but-identical-underlying-type%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3















      As mentioned by mkopriva, type-conversion is needed to compare the values.



      https://tour.golang.org/basics/13



      func areEqual(sid SomeID, oid OtherID) bool 
      return uint8(sid) == uint8(oid)






      share|improve this answer





























        3















        As mentioned by mkopriva, type-conversion is needed to compare the values.



        https://tour.golang.org/basics/13



        func areEqual(sid SomeID, oid OtherID) bool 
        return uint8(sid) == uint8(oid)






        share|improve this answer



























          3














          3










          3









          As mentioned by mkopriva, type-conversion is needed to compare the values.



          https://tour.golang.org/basics/13



          func areEqual(sid SomeID, oid OtherID) bool 
          return uint8(sid) == uint8(oid)






          share|improve this answer













          As mentioned by mkopriva, type-conversion is needed to compare the values.



          https://tour.golang.org/basics/13



          func areEqual(sid SomeID, oid OtherID) bool 
          return uint8(sid) == uint8(oid)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 20:13









          octaviooctavio

          1848 bronze badges




          1848 bronze badges


























              0















              You can be less accurate, you don't need to remember underlying type



              func areEqual(sid SomeID, oid OtherID) bool 
              return sid == SomeID(oid)



              And you can be more generic



              func areEqual(x, y interface) (bool, error) 
              xv := reflect.ValueOf(x)
              yv := reflect.ValueOf(y)
              if yv.Type().ConvertibleTo(xv.Type())
              return xv.Interface() == yv.Convert(xv.Type()).Interface(), nil
              else
              return false, errors.New("Types are mismatched")




              Playground






              share|improve this answer





























                0















                You can be less accurate, you don't need to remember underlying type



                func areEqual(sid SomeID, oid OtherID) bool 
                return sid == SomeID(oid)



                And you can be more generic



                func areEqual(x, y interface) (bool, error) 
                xv := reflect.ValueOf(x)
                yv := reflect.ValueOf(y)
                if yv.Type().ConvertibleTo(xv.Type())
                return xv.Interface() == yv.Convert(xv.Type()).Interface(), nil
                else
                return false, errors.New("Types are mismatched")




                Playground






                share|improve this answer



























                  0














                  0










                  0









                  You can be less accurate, you don't need to remember underlying type



                  func areEqual(sid SomeID, oid OtherID) bool 
                  return sid == SomeID(oid)



                  And you can be more generic



                  func areEqual(x, y interface) (bool, error) 
                  xv := reflect.ValueOf(x)
                  yv := reflect.ValueOf(y)
                  if yv.Type().ConvertibleTo(xv.Type())
                  return xv.Interface() == yv.Convert(xv.Type()).Interface(), nil
                  else
                  return false, errors.New("Types are mismatched")




                  Playground






                  share|improve this answer













                  You can be less accurate, you don't need to remember underlying type



                  func areEqual(sid SomeID, oid OtherID) bool 
                  return sid == SomeID(oid)



                  And you can be more generic



                  func areEqual(x, y interface) (bool, error) 
                  xv := reflect.ValueOf(x)
                  yv := reflect.ValueOf(y)
                  if yv.Type().ConvertibleTo(xv.Type())
                  return xv.Interface() == yv.Convert(xv.Type()).Interface(), nil
                  else
                  return false, errors.New("Types are mismatched")




                  Playground







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 13:11









                  UvelichitelUvelichitel

                  6,1591 gold badge9 silver badges29 bronze badges




                  6,1591 gold badge9 silver badges29 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385539%2fcompare-two-values-of-different-type-but-identical-underlying-type%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript