src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_misc.d.elv (about)

     1  # Accepts arbitrary arguments and options and does exactly nothing.
     2  #
     3  # Examples:
     4  #
     5  # ```elvish-transcript
     6  # ~> nop
     7  # ~> nop a b c
     8  # ~> nop &k=v
     9  # ```
    10  #
    11  # Etymology: Various languages, in particular NOP in
    12  # [assembly languages](https://en.wikipedia.org/wiki/NOP).
    13  fn nop {|&any-opt= @value| }
    14  
    15  # Output the kinds of `$value`s. Example:
    16  #
    17  # ```elvish-transcript
    18  # ~> kind-of lorem [] [&]
    19  # ▶ string
    20  # ▶ list
    21  # ▶ map
    22  # ```
    23  #
    24  # The terminology and definition of "kind" is subject to change.
    25  fn kind-of {|@value| }
    26  
    27  # Output a function that takes no arguments and outputs `$value`s when called.
    28  # Examples:
    29  #
    30  # ```elvish-transcript
    31  # ~> var f = (constantly lorem ipsum)
    32  # ~> $f
    33  # ▶ lorem
    34  # ▶ ipsum
    35  # ```
    36  #
    37  # The above example is equivalent to simply `var f = { put lorem ipsum }`;
    38  # it is most useful when the argument is **not** a literal value, e.g.
    39  #
    40  # ```elvish-transcript
    41  # //eval fn whoami { echo elf }
    42  # ~> var f = (constantly (whoami))
    43  # ~> $f
    44  # ▶ elf
    45  # ~> $f
    46  # ▶ elf
    47  # ```
    48  #
    49  # The above code only calls `whoami` once when defining `$f`. In contrast, if
    50  # `$f` is defined as `var f = { put (whoami) }`, every time you invoke `$f`,
    51  # `whoami` will be called.
    52  #
    53  # Etymology: [Clojure](https://clojuredocs.org/clojure.core/constantly).
    54  fn constantly {|@value| }
    55  
    56  # Calls `$fn` with `$args` as the arguments, and `$opts` as the option. Useful
    57  # for calling a function with dynamic option keys.
    58  #
    59  # Example:
    60  #
    61  # ```elvish-transcript
    62  # ~> var f = {|a &k1=v1 &k2=v2| put $a $k1 $k2 }
    63  # ~> call $f [foo] [&k1=bar]
    64  # ▶ foo
    65  # ▶ bar
    66  # ▶ v2
    67  # ```
    68  fn call {|fn args opts| }
    69  
    70  # Output what `$command` resolves to in symbolic form. Command resolution is
    71  # described in the [language reference](language.html#ordinary-command).
    72  #
    73  # Example:
    74  #
    75  # ```elvish-transcript
    76  # ~> resolve echo
    77  # ▶ '$echo~'
    78  # ~> fn f { }
    79  # ~> resolve f
    80  # ▶ '$f~'
    81  # ~> resolve cat
    82  # ▶ '(external cat)'
    83  # ```
    84  fn resolve {|command| }
    85  
    86  # Evaluates `$code`, which should be a string. The evaluation happens in a
    87  # new, restricted namespace, whose initial set of variables can be specified by
    88  # the `&ns` option. After evaluation completes, the new namespace is passed to
    89  # the callback specified by `&on-end` if it is not nil.
    90  #
    91  # The namespace specified by `&ns` is never modified; it will not be affected
    92  # by the creation or deletion of variables by `$code`. However, the values of
    93  # the variables may be mutated by `$code`.
    94  #
    95  # If the `&ns` option is `$nil` (the default), a temporary namespace built by
    96  # amalgamating the local and [upvalue scopes](language.html#upvalues) of the
    97  # caller is used.
    98  #
    99  # If `$code` fails to parse or compile, the parse error or compilation error is
   100  # raised as an exception.
   101  #
   102  # Basic examples that do not modify the namespace or any variable:
   103  #
   104  # ```elvish-transcript
   105  # ~> eval 'put x'
   106  # ▶ x
   107  # ~> var x = foo
   108  # ~> eval 'put $x'
   109  # ▶ foo
   110  # ~> var ns = (ns [&x=bar])
   111  # ~> eval &ns=$ns 'put $x'
   112  # ▶ bar
   113  # ```
   114  #
   115  # Examples that modify existing variables:
   116  #
   117  # ```elvish-transcript
   118  # ~> var y = foo
   119  # ~> eval 'set y = bar'
   120  # ~> put $y
   121  # ▶ bar
   122  # ```
   123  #
   124  # Examples that creates new variables and uses the callback to access it:
   125  #
   126  # ```elvish-transcript
   127  # ~> eval 'var z = lorem'
   128  # ~> put $z
   129  # Compilation error: variable $z not found
   130  #   [tty]:1:5-6: put $z
   131  # ~> var saved-ns = $nil
   132  # ~> eval &on-end={|ns| set saved-ns = $ns } 'var z = lorem'
   133  # ~> put $saved-ns[z]
   134  # ▶ lorem
   135  # ```
   136  #
   137  # Note that when using variables from an outer scope, only those
   138  # that have been referenced are captured as upvalues (see [closure
   139  # semantics](language.html#closure-semantics)) and thus accessible to `eval`:
   140  #
   141  # ```elvish-transcript
   142  # //skip-test
   143  # // Skipping since the error contains the context-sensitive "[eval 2]"
   144  # ~> var a b
   145  # ~> fn f {|code| nop $a; eval $code }
   146  # ~> f 'echo $a'
   147  # $nil
   148  # ~> f 'echo $b'
   149  # Exception: Compilation error: variable $b not found
   150  #   [eval 2]:1:6-7: echo $b
   151  #   [tty]:1:22-32: fn f {|code| nop $a; eval $code }
   152  #   [tty]:1:1-11: f 'echo $b'
   153  # ```
   154  fn eval {|code &ns=$nil &on-end=$nil| }
   155  
   156  #//in-temp-dir
   157  # Imports a module, and outputs the namespace for the module.
   158  #
   159  # Most code should use the [use](language.html#importing-modules-with-use)
   160  # special command instead.
   161  #
   162  # Examples:
   163  #
   164  # ```elvish-transcript
   165  # ~> echo 'var x = value' > a.elv
   166  # ~> put (use-mod ./a)[x]
   167  # ▶ value
   168  # ```
   169  fn use-mod {|use-spec| }
   170  
   171  # Shows the given deprecation message to stderr. If called from a function
   172  # or module, also shows the call site of the function or import site of the
   173  # module. Does nothing if the combination of the call site and the message has
   174  # been shown before.
   175  #
   176  # ```elvish-transcript
   177  # ~> deprecate msg
   178  # Deprecation: msg
   179  #   [tty]:1:1-13: deprecate msg
   180  # ~> fn f { deprecate msg }
   181  # ~> f
   182  # Deprecation: msg
   183  #   [tty]:1:1-1: f
   184  # ~> f # a different call site; shows deprecate message
   185  # Deprecation: msg
   186  #   [tty]:1:1-50: f # a different call site; shows deprecate message
   187  # ~> fn g { f }
   188  # ~> g
   189  # Deprecation: msg
   190  #   [tty]:1:8-9: fn g { f }
   191  # ~> g # same call site, no more deprecation message
   192  # ```
   193  fn deprecate {|msg| }
   194  
   195  #doc:show-unstable
   196  # Output all IP addresses of the current host.
   197  #
   198  # This should be part of a networking module instead of the builtin module.
   199  fn -ifaddrs { }