pkg/eval/vals: Replace MakeListFromStrings with a generic MakeListSlice.

This commit is contained in:
Qi Xiao 2022-06-17 16:50:38 +01:00
parent 01756ef57a
commit d48b6f1843
4 changed files with 11 additions and 15 deletions

View File

@ -182,9 +182,9 @@ type closureFields struct{ c *Closure }
func (closureFields) IsStructMap() {}
func (cf closureFields) ArgNames() vals.List { return vals.MakeListFromStrings(cf.c.ArgNames...) }
func (cf closureFields) ArgNames() vals.List { return vals.MakeListSlice(cf.c.ArgNames) }
func (cf closureFields) RestArg() string { return strconv.Itoa(cf.c.RestArg) }
func (cf closureFields) OptNames() vals.List { return vals.MakeListFromStrings(cf.c.OptNames...) }
func (cf closureFields) OptNames() vals.List { return vals.MakeListSlice(cf.c.OptNames) }
func (cf closureFields) Src() parse.Source { return cf.c.SrcMeta }
func (cf closureFields) OptDefaults() vals.List {

View File

@ -284,7 +284,7 @@ func (ev *Evaler) addNumBgJobs(delta int) {
// the given slice. This method must be called before the Evaler is used to
// evaluate any code.
func (ev *Evaler) SetArgs(args []string) {
ev.Args = vals.MakeListFromStrings(args...)
ev.Args = vals.MakeListSlice(args)
}
// AddBeforeChdir adds a function to run before changing directory. This method

View File

@ -18,6 +18,11 @@ var EmptyList = vector.Empty
// MakeList creates a new List from values.
func MakeList(vs ...any) vector.Vector {
return MakeListSlice(vs)
}
// MakeListSlice creates a new List from a slice.
func MakeListSlice[T any](vs []T) vector.Vector {
vec := vector.Empty
for _, v := range vs {
vec = vec.Conj(v)
@ -25,15 +30,6 @@ func MakeList(vs ...any) vector.Vector {
return vec
}
// MakeListFromStrings creates a new List from strings.
func MakeListFromStrings(ss ...string) vector.Vector {
vec := vector.Empty
for _, s := range ss {
vec = vec.Conj(s)
}
return vec
}
// Map is an alias for the underlying type used for maps in Elvish.
type Map = hashmap.Map

View File

@ -174,7 +174,7 @@ func parse(argsVal vals.List, specsVal vals.List) (vals.Map, vals.List, error) {
fs.VisitAll(func(f *flag.Flag) {
m = m.Assoc(f.Name, f.Value.(flag.Getter).Get())
})
return m, vals.MakeListFromStrings(fs.Args()...), nil
return m, vals.MakeListSlice(fs.Args()), nil
}
func newFlagSet(name string) *flag.FlagSet {
@ -213,7 +213,7 @@ func (lf *listFlag) String() string { return vals.ToString(lf.value) }
func (lf *listFlag) Get() any { return lf.value }
func (lf *listFlag) Set(s string) error {
lf.value = vals.MakeListFromStrings(strings.Split(s, ",")...)
lf.value = vals.MakeListSlice(strings.Split(s, ","))
return nil
}
@ -374,5 +374,5 @@ func parseGetopt(opts parseGetoptOptions, argsVal vals.List, specsVal vals.List)
"long", flag.Long))
}
return flagsList, vals.MakeListFromStrings(nonFlagArgs...), nil
return flagsList, vals.MakeListSlice(nonFlagArgs), nil
}