Added types package
All checks were successful
/ test (push) Successful in 15s

This commit is contained in:
Eduard Urbach 2025-06-20 16:16:48 +02:00
parent ed6ae1d306
commit 714a722aaa
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
7 changed files with 189 additions and 1 deletions

47
src/types/Is.go Normal file
View file

@ -0,0 +1,47 @@
package types
// Is returns true if the encountered type `a` can be converted into the expected type `b`.
func Is(a Type, b Type) bool {
if a == b || b == Any || a == nil {
return true
}
aPointer, aIsPointer := a.(*Pointer)
bPointer, bIsPointer := b.(*Pointer)
if aIsPointer && bIsPointer && (bPointer.To == Any || aPointer.To == bPointer.To) {
return true
}
aArray, aIsArray := a.(*Array)
if aIsArray && bIsPointer && (bPointer.To == Any || aArray.Of == bPointer.To) {
return true
}
bArray, bIsArray := b.(*Array)
if aIsArray && bIsArray && (bArray.Of == Any || aArray.Of == bArray.Of) {
return true
}
if a == AnyInt {
switch b {
case Int64, Int32, Int16, Int8, UInt64, UInt32, UInt16, UInt8, AnyInt:
return true
default:
return false
}
}
if b == AnyInt {
switch a {
case Int64, Int32, Int16, Int8, UInt64, UInt32, UInt16, UInt8, AnyInt:
return true
default:
return false
}
}
return false
}