Commit 2a230abe authored by Eric Myhre's avatar Eric Myhre

Add ReprKind method to Type interface.

Having a Name() interface method might also have been useful, but at
the moment, it's too annoying.  Maybe I'll come back to this and add a
ton of constructors for each kind of type and make all their fields
private scope, which would solve the name collision... maybe.  Later.
(This is all expected to be implementation-internal stuff in the long
run rather than anything user-facing API, so it's up for debate how
much polishing it's actually worth.  Unless that changes!)

The ReprKind method is *usually* pretty predestined based on the kind
of type in the first place, but a few cases are interesting.

... *Especially* kinded unions.  I'm a little alarmed at the break of
pattern, there.  Hopefully that doesn't manifest too much complexity
down the road.  But if it does... eh, well... nature of the beast.
Kinded unions are definitely a useful feature.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent 8ca9804f
package typed
func (TypeBool) ReprKind() ReprKind {
return ReprKind_Bool
}
func (TypeString) ReprKind() ReprKind {
return ReprKind_String
}
func (TypeBytes) ReprKind() ReprKind {
return ReprKind_Bytes
}
func (TypeInt) ReprKind() ReprKind {
return ReprKind_Int
}
func (TypeFloat) ReprKind() ReprKind {
return ReprKind_Float
}
func (TypeMap) ReprKind() ReprKind {
return ReprKind_Map
}
func (TypeList) ReprKind() ReprKind {
return ReprKind_List
}
func (TypeLink) ReprKind() ReprKind {
return ReprKind_Link
}
func (tv TypeUnion) ReprKind() ReprKind {
// REVIEW: this may fib; has the bizarre property of being dependent on the *concrete value* for kinded unions!
if tv.Style == UnionStyle_Kinded {
return ReprKind_Invalid
} else {
return ReprKind_Map
}
}
func (tv TypeObject) ReprKind() ReprKind {
if tv.TupleStyle {
return ReprKind_List
} else {
return ReprKind_Map
}
}
func (TypeEnum) ReprKind() ReprKind {
return ReprKind_String
}
...@@ -3,10 +3,24 @@ package typed ...@@ -3,10 +3,24 @@ package typed
type TypeName string type TypeName string
type Type interface { type Type interface {
// Name() TypeName // Name() TypeName // annoying name collision.
// ReprKind() ReprKind ReprKind() ReprKind
} }
var (
_ Type = TypeBool{}
_ Type = TypeString{}
_ Type = TypeBytes{}
_ Type = TypeInt{}
_ Type = TypeFloat{}
_ Type = TypeMap{}
_ Type = TypeList{}
_ Type = TypeLink{}
_ Type = TypeUnion{}
_ Type = TypeObject{}
_ Type = TypeEnum{}
)
type TypeBool struct { type TypeBool struct {
Name TypeName Name TypeName
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment