FAQ
I've been experimenting about what go allows about types and functions, and
I was not expecting this code to be legal.

package testPkg

//Type declaration of a function type
type pred func(int) bool

func isEven(x int) bool {
  return x%2 == 0
}

//OK: explicit conversion
var someFunc pred = pred(isEven)

//Somehow manages to work without a type conversion
var someFunc2 pred = isEven

func apply(f pred, val int) bool {
  return f(val)
}

//Obviously works
var result bool = apply(pred(isEven), 0)

//What??? This obviously is a compile error!
var result2 bool = apply(isEven, 0)

Go usually requires type conversions for type mismatches, but this code
somehow compiles cleanly.

Did go do an implicit type conversion from func(int) bool to pred , or is pred
really a synonym of func(int) bool ?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jesse McNelis at Nov 23, 2014 at 3:20 am

    On Sun, Nov 23, 2014 at 2:10 PM, Theemathas Chirananhavat wrote:
    Go usually requires type conversions for type mismatches, but this code
    somehow compiles cleanly.

    Did go do an implicit type conversion from func(int) bool to pred , or is
    pred really a synonym of func(int) bool ?
    Go only requires type conversion between values of named types.
    http://golang.org/ref/spec#Types

    func(int) bool isn't a named type.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Theemathas Chirananhavat at Nov 23, 2014 at 3:29 am

    On Sunday, November 23, 2014 10:20:59 AM UTC+7, Jesse McNelis wrote:
    On Sun, Nov 23, 2014 at 2:10 PM, Theemathas Chirananhavat
    <theem...@gmail.com <javascript:>> wrote:
    Go usually requires type conversions for type mismatches, but this code
    somehow compiles cleanly.

    Did go do an implicit type conversion from func(int) bool to pred , or is
    pred really a synonym of func(int) bool ?
    Go only requires type conversion between values of named types.
    http://golang.org/ref/spec#Types

    func(int) bool isn't a named type.
    Thank you. This language design seems a bit weird to me, but I guess it is
    the most practical way to do it.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 23, '14 at 3:10a
activeNov 23, '14 at 3:29a
posts3
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase