help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing optional arguments for use with internal functions


From: Emanuel Berg
Subject: Re: Passing optional arguments for use with internal functions
Date: Thu, 03 Aug 2023 17:46:39 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Here are both programs. The first one is under lexical scope.
If you use it, function arguments are always lexical.

But the second program, under dynamic scope - yes, what does
that show exactly?

The case with (4 4) is the most interesting because it ignores
the global/dynamic gsdd - but its own gsdd is nevertheless
dynamic as well?

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/geh.el

(defvar gsd 1)

(defun test-gsd-gsd ()
  gsd)

(defun test-gsd (&optional gsd)
  (or gsd (setq gsd 2))
  gsd)

;; (test-gsd-gsd) ; 1 - global/dynamic gsd
;; (test-gsd)     ; 2 - lexical argument gsd
;; (test-gsd 3)   ; 3 - lexical argument gsd

;; byte compile:
;;   Warning: global/dynamic var ‘gsd’ lacks a prefix
;;   Warning: Lexical argument shadows the dynamic variable gsd

;;; -*- lexical-binding: nil -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/geh-dyn.el

(defvar gsdd 3)

(defun test-gsdd-gsdd ()
  gsdd)

(defun test-gsdd (&optional gsdd)
  (or gsdd (setq gsdd 4))
  (list (test-gsdd-gsdd) gsdd) )

;; (test-gsdd-gsdd) ; 3 - global/dynamic gsdd
;; (test-gsdd)      ; (4 4) - same
;; (test-gsdd 5)    ; (5 5) - same

(provide 'geh-dyn)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]