github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/vals/len.go (about)

     1  package vals
     2  
     3  import "github.com/u-root/u-root/cmds/core/elvish/vector"
     4  
     5  // Lener wraps the Len method.
     6  type Lener interface {
     7  	// Len computes the length of the receiver.
     8  	Len() int
     9  }
    10  
    11  var _ Lener = vector.Vector(nil)
    12  
    13  // Len returns the length of the value, or -1 if the value does not have a
    14  // well-defined length. It is implemented for the builtin string type and types
    15  // satisfying the Lener interface. For other types, it returns -1.
    16  func Len(v interface{}) int {
    17  	switch v := v.(type) {
    18  	case Lener:
    19  		return v.Len()
    20  	case string:
    21  		return len(v)
    22  	}
    23  	return -1
    24  }