github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/eval/vars/callback.go (about)

     1  package vars
     2  
     3  type callback struct {
     4  	set func(interface{}) error
     5  	get func() interface{}
     6  }
     7  
     8  // FromSetGet makes a variable from a set callback and a get callback.
     9  func FromSetGet(set func(interface{}) error, get func() interface{}) Var {
    10  	return &callback{set, get}
    11  }
    12  
    13  func (cv *callback) Set(val interface{}) error {
    14  	return cv.set(val)
    15  }
    16  
    17  func (cv *callback) Get() interface{} {
    18  	return cv.get()
    19  }
    20  
    21  type roCallback func() interface{}
    22  
    23  // FromGet makes a variable from a get callback. The variable is read-only.
    24  func FromGet(get func() interface{}) Var {
    25  	return roCallback(get)
    26  }
    27  
    28  func (cv roCallback) Set(interface{}) error {
    29  	return errRoCannotBeSet
    30  }
    31  
    32  func (cv roCallback) Get() interface{} {
    33  	return cv()
    34  }