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;
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
add a comment |
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
4
First convert them back touint8
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 aSomeID
to aOtherID
shouldn't yield any meaningful result; like comparing atime.Month
to atime.Weekday
, which are bothint
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
add a comment |
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
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
go types compare
asked Mar 27 at 20:00
octaviooctavio
1848 bronze badges
1848 bronze badges
4
First convert them back touint8
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 aSomeID
to aOtherID
shouldn't yield any meaningful result; like comparing atime.Month
to atime.Weekday
, which are bothint
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
add a comment |
4
First convert them back touint8
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 aSomeID
to aOtherID
shouldn't yield any meaningful result; like comparing atime.Month
to atime.Weekday
, which are bothint
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
add a comment |
2 Answers
2
active
oldest
votes
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)
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Mar 27 at 20:13
octaviooctavio
1848 bronze badges
1848 bronze badges
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 28 at 13:11
UvelichitelUvelichitel
6,1591 gold badge9 silver badges29 bronze badges
6,1591 gold badge9 silver badges29 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 aOtherID
shouldn't yield any meaningful result; like comparing atime.Month
to atime.Weekday
, which are bothint
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