You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given v := constant.Make(big.NewInt(10)), constant.Int64Val(v) returns (10, false), but I'd expect it to return (10, true). See https://play.golang.org/p/SPzai9ii_7_f.
This is because go/constant expects that intVal will only be used to represent values that don't fit into an int64, because it always uses makeInt to convert a big.Int into a Value, which satisfies this expectation.
I see three possible solutions:
Change Make to use makeInt (and maybe makeRat and makeFloat) to ensure the internal representations match how go/constant would have represented them normally. This will require TestMake to be updated though, because Val(Make(big.NewInt(10))) would now return an int64 instead of *big.Int.
Change Int64Val to return Int64Val to return x.val.IsInt64() instead of false for intVal.
Document that callers of Make should use an int64 instead of big.Int when possible, and maybe panic if provided a big.Int that fits int64.
The text was updated successfully, but these errors were encountered:
mdempsky
changed the title
go/constant: constant.Int64Val(constant.Make(big.NewInt(10))) returns (10, false), not (10, true)
go/constant: Int64Val(Make(big.NewInt(10))) returns (10, false), not (10, true)
Nov 16, 2020
Given
v := constant.Make(big.NewInt(10))
,constant.Int64Val(v)
returns(10, false)
, but I'd expect it to return(10, true)
. See https://play.golang.org/p/SPzai9ii_7_f.This is because go/constant expects that
intVal
will only be used to represent values that don't fit into anint64
, because it always usesmakeInt
to convert abig.Int
into aValue
, which satisfies this expectation.I see three possible solutions:
Change
Make
to usemakeInt
(and maybemakeRat
andmakeFloat
) to ensure the internal representations match howgo/constant
would have represented them normally. This will requireTestMake
to be updated though, becauseVal(Make(big.NewInt(10)))
would now return anint64
instead of*big.Int
.Change
Int64Val
to returnInt64Val
to returnx.val.IsInt64()
instead offalse
forintVal
.Document that callers of
Make
should use anint64
instead ofbig.Int
when possible, and maybe panic if provided abig.Int
that fitsint64
./cc @griesemer @stamblerre
The text was updated successfully, but these errors were encountered: