Is there a difference between type casting and type conversion?

589 views
Skip to first unread message

Gabriel Aszalos

unread,
Nov 25, 2014, 7:03:41 PM11/25/14
to golan...@googlegroups.com
In Go, it is called "Conversion". The word type casting is never mentioned. Wikipedia claims that they are synonyms: "In computer sciencetype conversiontypecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. "

Jesse McNelis

unread,
Nov 25, 2014, 7:39:58 PM11/25/14
to Gabriel Aszalos, golang-nuts
In C typecasting allows you to take a value with a type and pretend
it's of any other type which isn't memory safe.
In Java typecasting allows you to take a value with a type and convert
it to a value of a parent type.
Since C and Java disagree about the meaning of 'typecast' it's a word to avoid.

If your background is Java then Go's conversion and Java's
'typecasting' is the same apart from Go not having a type hierarchy.
Go just has rules that specify which values of which types can be
converted to values of which other types.
http://golang.org/ref/spec#Conversions

Dave Cheney

unread,
Nov 25, 2014, 8:21:20 PM11/25/14
to golan...@googlegroups.com
It's called conversion in Go, as distinct from a cast to remind everyone that a conversion always makes a copy of the argument.

Jeffrey 'jf' Lim

unread,
Nov 26, 2014, 3:11:44 AM11/26/14
to golang-nuts
haha. I've been meaning to ask the same question, but only just
recently figured it out. Dave has an excellent insight (below) which I
never thought of; but there is also one other distinction, and why the
two (conversion, and casting) are separate things in the language:
conversion can potentially *change* your *value* (see the examples at
http://golang.org/ref/spec#Conversions), while casting only changes
the record of the *type* as per the Go runtime, but does not -- and
will not! -- modify the actual contents.

I think casting (when not casting between pointers!) should also make
a copy given Go's type system, but I have yet to produce code that
will conclusively prove this to my satisfaction. (The problem is that
normal assignments (short of pointer assignments), are already a copy;
but perhaps that's a "might as well" sort of thing....)

-jf

Gabriel Aszalos

unread,
Nov 26, 2014, 3:22:09 AM11/26/14
to golang-nuts
Thank you gentlemen. Those are great answers. 

I suspected that was the case. As a general programming concept, they seem to be the same thing, but, one might make confusions when using the term "type casting" based on his or her programming background, so the term "conversion" is used instead to avoid this. Makes sense!

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/dwSPKq9YDso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Gillette

unread,
Nov 26, 2014, 3:43:49 AM11/26/14
to golan...@googlegroups.com
In this case, I suspect you're referring to what the Go spec would call "type assertion" when your using the term casting.

Type conversion is always static -- it can (and is) fully resolved at compile time. In contrast, type assertion always involves an interface as the source operand and, in the general case, can only be resolved at runtime.

If the destination operand of a type assertion is itself an interface containing a subset of the source's methods (such as going from an io.ReadWriter to an io.Reader, or from anything to an empty interface), assignment or conversion should be used instead. For example:

var rw io.ReadWriter = x
r := rw.(io.Reader) // bad, since we already know that rw is an io.Reader also (let's avoid runtime work)

Instead, we can use either of the following:

var rw io.ReadWriter = x
r := io.Reader(rw) // okay -- using explicit conversion which the compiler will verify
 
var rw io.ReadWriter = x
var r io.Reader = rw // also okay -- using assignability rules which the compiler will verify

However, you would use type assertion when moving in the opposite direction (from io.Reader to io.ReadWriter), when changing between unrelated interfaces (i.e. io.Reader <-> fmt.Stringer), or when unboxing an interface into a concrete type.

Michael Jones

unread,
Nov 26, 2014, 6:30:19 AM11/26/14
to Kevin Gillette, golang-nuts

The Wikipedia sentence is not encyclopedic with regard to BCPL, B, C, C++ casts through pointers.

--
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.
Reply all
Reply to author
Forward
0 new messages