src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/vars/read_only.go (about) 1 package vars 2 3 import ( 4 "src.elv.sh/pkg/eval/errs" 5 ) 6 7 type readOnly struct { 8 value any 9 } 10 11 // NewReadOnly creates a variable that is read-only and always returns an error 12 // on Set. 13 func NewReadOnly(v any) Var { 14 return readOnly{v} 15 } 16 17 func (rv readOnly) Set(val any) error { 18 return errs.SetReadOnlyVar{} 19 } 20 21 func (rv readOnly) Get() any { 22 return rv.value 23 } 24 25 // IsReadOnly returns whether v is a read-only variable. 26 func IsReadOnly(v Var) bool { 27 switch v.(type) { 28 case readOnly: 29 return true 30 case roCallback: 31 return true 32 default: 33 return false 34 } 35 }