From 3b46e88734c5860965ef93f01be535095dd60257 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 11 Oct 2023 20:38:35 -0400 Subject: [PATCH] lang: types: Improve docs for New and initialize the list If we use this to generate a zero value, we want to make sure it's completely initialized in case we use it subsequently. We also improve the docs at the same time. --- lang/types/type.go | 6 +++++- lang/types/value.go | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lang/types/type.go b/lang/types/type.go index 9461b1f9..aa72ca86 100644 --- a/lang/types/type.go +++ b/lang/types/type.go @@ -550,8 +550,12 @@ func NewType(s string) *Type { return nil // error (this also matches the empty string as input) } -// New creates a new Value of this type. +// New creates a new Value of this type. It will represent the "zero" value. It +// panics if you give it a malformed type. func (obj *Type) New() Value { + if obj == nil { + panic("malformed type") + } switch obj.Kind { case KindBool: return NewBool() diff --git a/lang/types/value.go b/lang/types/value.go index 9281d974..1edd23e9 100644 --- a/lang/types/value.go +++ b/lang/types/value.go @@ -774,6 +774,7 @@ func NewList(t *Type) *ListValue { return nil // sanity check } return &ListValue{ + V: []Value{}, T: t, } }