Safe cast from int64 to int8

2,333 views
Skip to first unread message

DeusModus

unread,
Apr 7, 2015, 12:29:10 PM4/7/15
to golan...@googlegroups.com
Hi!

Is there any possibility to cast int64 to int8 and retrieve error if int64 type val overflows int8?

Josh Bleecher Snyder

unread,
Apr 7, 2015, 12:34:58 PM4/7/15
to DeusModus, golang-nuts
> Is there any possibility to cast int64 to int8 and retrieve error if int64
> type val overflows int8?

You can convert back and check whether the value was preserved.

http://play.golang.org/p/wnZj8n8Ply

-josh

Ian Lance Taylor

unread,
Apr 7, 2015, 12:35:11 PM4/7/15
to DeusModus, golang-nuts
On Tue, Apr 7, 2015 at 9:12 AM, DeusModus <maxime...@gmail.com> wrote:
>
> Is there any possibility to cast int64 to int8 and retrieve error if int64
> type val overflows int8?

i8 := int8(i64)
if int64(i8) != i64 {
// oh noes!!!
}

Ian

Joe Taber

unread,
Apr 7, 2015, 12:39:23 PM4/7/15
to golan...@googlegroups.com
From the spec[1], "When converting between integer types... The conversion always yields a valid value; there is no indication of overflow."

You'll have to check afterwards as Josh or Ian mentioned.

Axel Wagner

unread,
Apr 7, 2015, 1:01:06 PM4/7/15
to golan...@googlegroups.com
Or you use the math-package: http://play.golang.org/p/kUTi4yzisV
> --
> 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 golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

DeusModus

unread,
Apr 7, 2015, 7:11:07 PM4/7/15
to golan...@googlegroups.com
Thought that there was more elegant way.
Anyway,
thank you guys.

PS: I'll write library for this purpose if there is no any.

Konstantin Khomoutov

unread,
Apr 9, 2015, 6:38:56 AM4/9/15
to DeusModus, golan...@googlegroups.com
Please, don't do that.
There's no need to write a library to provide a function implementing
three lines of code. Even if there will be N functions,
each implementing these three lines of code -- one for each integral
type. Functions might be not inlined for some reason and won't be
optimized. Another problem is dependencies. Please read [1] and watch
the video it refers to.

1. https://groups.google.com/d/msg/golang-nuts/R_lqsTTBh6I/DWIwfuMXjfgJ

Klaus Post

unread,
Apr 9, 2015, 8:33:43 AM4/9/15
to golan...@googlegroups.com, maxime...@gmail.com
On Thursday, 9 April 2015 12:38:56 UTC+2, Konstantin Khomoutov wrote:
Please, don't do that.

Why not - then 4 lines of code will become much shorter.. something like 4 lines of code ;)

Seriously, though, look at how you will likely use it in your actual code. As you can see you don't really gain anything, and you lose clarity of what happens in your code beside the inlining issue Konstantin pointed out.

 // Inline, 4 lines.
 
if int64(int8(x)) != x {
   
return ErrTruncated
 
}
 v
:= int8(x)
 
 
// With "package", 4 lines.
 v
, err := caster.Int64ToInt8(x)
 
if err != nil {
   
return err
 
}


 

Dave Cheney

unread,
Apr 9, 2015, 8:58:57 AM4/9/15
to golan...@googlegroups.com, maxime...@gmail.com
Any Go programmer can read the first example with no training.

Only a Go programmer who has derailed themselves to looking up the documentation of your caster package.

Brevity is not a ends in itself.
Reply all
Reply to author
Forward
0 new messages