On Thu, Oct 19, 2023 at 9:03 PM Nurahmadie Nurahmadie <nurahma...@gmail.com>
wrote:

> Adding methods to a primitive type, or more generally adding methods
>> to a type defined in a different package, would cause different
>> packages to behave differently depending on whether they see the
>> methods.  That would be confusing.  It would meant that type
>> assertions would sometimes succeed and sometimes fail, depending on
>> the exact sequence of function calls used.
>>
>> Ian
>>
>
> Understood, granted due to Go structural typing it's easy to confuse where
> the methods are coming from.
>
> but given the following, for example,
>
> // this allow new methods to be bounded to `String`
> type String string
>
> func Display(s string) {
>         fmt.Prinln(s)
> }
>
> func main() {
>         s := String("hello")
>         // this wont work, even though there is enough information that
> `string` is the underlying type of `String`
>         // but if we use `type String = string`, this will work, but no
> additional method binding is allowed.
>         Display(s)
>
>         // for `type String string`, we have to downcast it manually, but
> we can bind new methods
>         Display(string(s)) // this works
> }
>
> Is it not possible to have both _auto_ downcasting and new method binding
> to work in Go?
>

You have changed the question. You are no longer asking about defining
methods on a type derived from a primitive type. Non-aliased types are
deliberately distinct types that cannot be "auto downcast" or "auto
upcast". Arguably the biggest flaw of the C language was its automatic type
coercion. And I say that as someone who had been programming for several
years but didn't find a language I loved until I learned C sometime around
1984. What you are proposing would lead to a huge number of bugs that
result from the C/C++ behavior you are advocating for. And that ignores the
technical difficulties of doing what you propose unless Go becomes more
like C++. Which most Go users, and I'm sure everyone on the Go development
team, would argue is a bad idea.

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD__898HLt9V4qnj0kb0OLzYr78rVB%2BCNt%2BvWMMn0bLdLg%40mail.gmail.com.

Reply via email to