github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/vars/read_only.go (about) 1 package vars 2 3 import ( 4 "github.com/markusbkk/elvish/pkg/eval/errs" 5 ) 6 7 type readOnly struct { 8 value interface{} 9 } 10 11 // NewReadOnly creates a variable that is read-only and always returns an error 12 // on Set. 13 func NewReadOnly(v interface{}) Var { 14 return readOnly{v} 15 } 16 17 func (rv readOnly) Set(val interface{}) error { 18 return errs.SetReadOnlyVar{} 19 } 20 21 func (rv readOnly) Get() interface{} { 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 }