Skip to content

Commit

Permalink
Fixed type checks of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Sep 18, 2018
1 parent e8727eb commit 6c754dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Type = reflect.Type
type typesTable map[string]Type

var (
nilType = reflect.TypeOf(nil)
boolType = reflect.TypeOf(true)
numberType = reflect.TypeOf(float64(0))
textType = reflect.TypeOf("")
Expand Down Expand Up @@ -393,7 +394,10 @@ func funcType(ntype Type) (Type, bool) {
case reflect.Interface:
return interfaceType, true
case reflect.Func:
return ntype, true
if ntype.NumOut() > 0 {
return ntype.Out(0), true
}
return nilType, true
}

return nil, false
Expand Down
10 changes: 9 additions & 1 deletion type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var typeTests = []typeTest{
"Int in Int..Int",
"FieldStr == ''",
"FieldStr2 == ''",
"OkFn() and OkFn()",
"Foo.Fn() or Foo.Fn()",
}

var typeErrorTests = []typeErrorTest{
Expand Down Expand Up @@ -262,6 +264,10 @@ var typeErrorTests = []typeErrorTest{
"Int .. Ok",
"invalid operation: Int .. Ok (mismatched types int and bool)",
},
{
"NilFn() and OkFn()",
"invalid operation: NilFn() and OkFn() (mismatched types <nil> and bool)",
},
}

type abc interface {
Expand All @@ -272,7 +278,7 @@ type bar struct {
}
type foo struct {
Bar bar
Fn func()
Fn func() bool
Abc abc
}

Expand Down Expand Up @@ -304,6 +310,8 @@ type payload struct {
IntPtr *int
StrPtr *string
Foo2p **foo
OkFn func() bool
NilFn func()
}

func TestType(t *testing.T) {
Expand Down

0 comments on commit 6c754dc

Please sign in to comment.