all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Passing optional arguments for use with internal functions
@ 2023-07-27 23:45 uzibalqa
  2023-07-28 10:51 ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: uzibalqa @ 2023-07-27 23:45 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor

I have this function and want the ability to use the optional argument in
'lexif'.  I just added optional actm in reshuf so it is passed to 'lexif'
if the option is present.

Is this the way to allow optional arguments for 'lexif', or would I have 
additional tests to see whether 'actm' is present in 'reshuf' so that
I either call (lexif mutant) or (lexif mutant actm).

(defun reshuf (word n &optional actm)

  (let ( (wlis '()) )

    (add-to-list 'wlis word)
    (dotimes (i n)
      (let ((mutant (shuffle word)))

        (when (lexif mutant actm)
          (add-to-list 'wlis mutant) )))

    wlis))





^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-07-27 23:45 Passing optional arguments for use with internal functions uzibalqa
@ 2023-07-28 10:51 ` Emanuel Berg
  2023-07-28 16:42   ` Heime
  2023-07-28 20:02   ` [External] : " Drew Adams
  0 siblings, 2 replies; 16+ messages in thread
From: Emanuel Berg @ 2023-07-28 10:51 UTC (permalink / raw)
  To: help-gnu-emacs

uzibalqa wrote:

> Is this the way to allow optional arguments for 'lexif', or
> would I have additional tests [...]

Optional arguments defaults to nil, if that is an acceptable
value you can use it as you do, if not you can check if it is
nil first thing in the function and if it is, set it to
something that works, e.g.

(or arg (setq arg 0))

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-07-28 10:51 ` Emanuel Berg
@ 2023-07-28 16:42   ` Heime
  2023-07-28 17:11     ` Emanuel Berg
  2023-07-28 20:02   ` [External] : " Drew Adams
  1 sibling, 1 reply; 16+ messages in thread
From: Heime @ 2023-07-28 16:42 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

------- Original Message -------
On Friday, July 28th, 2023 at 10:51 PM, Emanuel Berg <incal@dataswamp.org> wrote:


> uzibalqa wrote:
> 
> > Is this the way to allow optional arguments for 'lexif', or
> > would I have additional tests [...]
> 
> 
> Optional arguments defaults to nil, if that is an acceptable
> value you can use it as you do, if not you can check if it is
> nil first thing in the function and if it is, set it to
> something that works, e.g.
> 
> (or arg (setq arg 0))

Does the condition you specified work whether the binding is lexical or dynamic ?
 
> --
> underground experts united
> https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-07-28 16:42   ` Heime
@ 2023-07-28 17:11     ` Emanuel Berg
  2023-07-30 16:09       ` uzibalqa
  0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2023-07-28 17:11 UTC (permalink / raw)
  To: help-gnu-emacs

Heime wrote:

>> Optional arguments defaults to nil, if that is an
>> acceptable value you can use it as you do, if not you can
>> check if it is nil first thing in the function and if it
>> is, set it to something that works, e.g.
>> 
>> (or arg (setq arg 0))
>
> Does the condition you specified work whether the binding is
> lexical or dynamic ?

If there is an optional argument with that name yes, since
formal parameters (function arguments) are always lexical.

;;; -*- lexical-binding: nil -*-

(defvar arg)
(setq arg 2)

(defun echo-arg (&optional arg)
  (or arg (setq arg 1))
  (message "%s" arg) )

;; (echo-arg)

This will still echo 1.

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* RE: [External] : Re: Passing optional arguments for use with internal functions
  2023-07-28 10:51 ` Emanuel Berg
  2023-07-28 16:42   ` Heime
@ 2023-07-28 20:02   ` Drew Adams
  2023-07-28 20:42     ` uzibalqa
  1 sibling, 1 reply; 16+ messages in thread
From: Drew Adams @ 2023-07-28 20:02 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs@gnu.org

> (or arg (setq arg 0))

aka (setq arg (or arg 0))

And if the context doesn't care
about the return value I'd make
that clear, using:

(unless arg (setq arg 0))



^ permalink raw reply	[flat|nested] 16+ messages in thread

* RE: [External] : Re: Passing optional arguments for use with internal functions
  2023-07-28 20:02   ` [External] : " Drew Adams
@ 2023-07-28 20:42     ` uzibalqa
  0 siblings, 0 replies; 16+ messages in thread
From: uzibalqa @ 2023-07-28 20:42 UTC (permalink / raw)
  To: Drew Adams; +Cc: Emanuel Berg, help-gnu-emacs@gnu.org


------- Original Message -------
On Saturday, July 29th, 2023 at 8:02 AM, Drew Adams <drew.adams@oracle.com> wrote:


> > (or arg (setq arg 0))
> 
> 
> aka (setq arg (or arg 0))
> 
> And if the context doesn't care
> about the return value I'd make
> that clear, using:
> 
> (unless arg (setq arg 0))

I understand.  But is the command (or arg (setq arg 0)) equally applicable
when using lexical or dynamic binding ?  Recently emacs is using lexical
binding on everything.



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-07-28 17:11     ` Emanuel Berg
@ 2023-07-30 16:09       ` uzibalqa
  2023-07-31 12:27         ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: uzibalqa @ 2023-07-30 16:09 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

------- Original Message -------
On Saturday, July 29th, 2023 at 5:11 AM, Emanuel Berg <incal@dataswamp.org> wrote:


> Heime wrote:
> 
> > > Optional arguments defaults to nil, if that is an
> > > acceptable value you can use it as you do, if not you can
> > > check if it is nil first thing in the function and if it
> > > is, set it to something that works, e.g.
> > > 
> > > (or arg (setq arg 0))
> > 
> > Does the condition you specified work whether the binding is
> > lexical or dynamic ?
> 
> 
> If there is an optional argument with that name yes, since
> formal parameters (function arguments) are always lexical.

Yes, I am discussing (or arg (setq arg 0)) in the context of
an optional argument by that name, for the purpose of setting
a default if it was not specified. 
 
> ;;; -- lexical-binding: nil --
> 
> (defvar arg)
> (setq arg 2)
> 
> (defun echo-arg (&optional arg)
> (or arg (setq arg 1))
> (message "%s" arg) )
> 
> ;; (echo-arg)
> 
> This will still echo 1.

Now that there has been a change from dynamic to lexical binding,
this becomes a problem.  What command should one use to set a default
value to an optional argument for the current lexical binding files ? 
 
> --
> underground experts united
> https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-07-30 16:09       ` uzibalqa
@ 2023-07-31 12:27         ` Emanuel Berg
  2023-08-02 18:01           ` uzibalqa
  0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2023-07-31 12:27 UTC (permalink / raw)
  To: help-gnu-emacs

uzibalqa wrote:

>> ;;; -- lexical-binding: nil --
>> 
>> (defvar arg)
>> (setq arg 2)
>> 
>> (defun echo-arg (&optional arg)
>> (or arg (setq arg 1))
>> (message "%s" arg) )
>> 
>> ;; (echo-arg)
>> 
>> This will still echo 1.
>
> Now that there has been a change from dynamic to lexical
> binding, this becomes a problem. What command should one use
> to set a default value to an optional argument for the
> current lexical binding files ?

The same, since function arguments are always lexical.

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-07-31 12:27         ` Emanuel Berg
@ 2023-08-02 18:01           ` uzibalqa
  2023-08-02 18:20             ` Emanuel Berg
  0 siblings, 1 reply; 16+ messages in thread
From: uzibalqa @ 2023-08-02 18:01 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs






Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, August 3rd, 2023 at 3:06 AM, Emanuel Berg <incal@dataswamp.org> wrote:


> uzibalqa wrote:
>
> > > ;;; -- lexical-binding: nil --
> > >
> > > (defvar arg)
> > > (setq arg 2)
> > >
> > > (defun echo-arg (&optional arg)
> > > (or arg (setq arg 1))
> > > (message "%s" arg) )
> > >
> > > ;; (echo-arg)
> > >
> > > This will still echo 1.
> >
> > Now that there has been a change from dynamic to lexical
> > binding, this becomes a problem. What command should one use
> > to set a default value to an optional argument for the
> > current lexical binding files ?
>
>
> The same, since function arguments are always lexical.

To be absolutely sure I understand.  If I want to default
a function optional argument if it is not used, the command
to that that is

(defun dothis x ()

  (or x (setq x 8))  ; Use 8 as default if x not passed to function or x is nil
  
  (do-something x)

)

And this would always work.  In will work if I use the lexical-binding directive 
at the top of a file.  And it would work if I use dynamic-binding directive at the 
top of a file.

Calling the command (dothis) will always use x with value 8, and executing (do-something 8) .


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



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 18:01           ` uzibalqa
@ 2023-08-02 18:20             ` Emanuel Berg
  2023-08-02 19:24               ` uzibalqa
  0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2023-08-02 18:20 UTC (permalink / raw)
  To: help-gnu-emacs

uzibalqa wrote:

> To be absolutely sure I understand. If I want to default
> a function optional argument if it is not used, the command
> to that that is
>
> (defun dothis x ()
>   (or x (setq x 8))  ; [...]
>   (do-something x)
> )
>
> And this would always work. In will work if I use the
> lexical-binding directive at the top of a file. And it would
> work if I use dynamic-binding directive at the top of
> a file.

Correct, as that wouldn't affect them because function formal
parameters (the argument placeholders) are always under
lexical/static scope.

> Calling the command (dothis) will always use x with value 8,
> and executing (do-something 8) .

Yes, you forgot about the &optional keyword in your example
but other than that it is correct.

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 18:20             ` Emanuel Berg
@ 2023-08-02 19:24               ` uzibalqa
  2023-08-02 19:53                 ` uzibalqa
  0 siblings, 1 reply; 16+ messages in thread
From: uzibalqa @ 2023-08-02 19:24 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs






Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, August 3rd, 2023 at 6:20 AM, Emanuel Berg <incal@dataswamp.org> wrote:


> uzibalqa wrote:
> 
> > To be absolutely sure I understand. If I want to default
> > a function optional argument if it is not used, the command
> > to that that is
> > 
> > (defun dothis x ()
> > (or x (setq x 8)) ; [...]
> > (do-something x)
> > )
> > 
> > And this would always work. In will work if I use the
> > lexical-binding directive at the top of a file. And it would
> > work if I use dynamic-binding directive at the top of
> > a file.
> 
> 
> Correct, as that wouldn't affect them because function formal
> parameters (the argument placeholders) are always under
> lexical/static scope.
> 
> > Calling the command (dothis) will always use x with value 8,
> > and executing (do-something 8) .
> 
> 
> Yes, you forgot about the &optional keyword in your example
> but other than that it is correct.

Right, it should have been  (defun dothis (&optional x)


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



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 19:24               ` uzibalqa
@ 2023-08-02 19:53                 ` uzibalqa
  2023-08-02 23:24                   ` Emanuel Berg
                                     ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: uzibalqa @ 2023-08-02 19:53 UTC (permalink / raw)
  To: uzibalqa; +Cc: Emanuel Berg, help-gnu-emacs

------- Original Message -------
On Thursday, August 3rd, 2023 at 7:24 AM, uzibalqa <uzibalqa@proton.me> wrote:

> Sent with Proton Mail secure email.
> 
> ------- Original Message -------
> On Thursday, August 3rd, 2023 at 6:20 AM, Emanuel Berg incal@dataswamp.org wrote:
> 
> > uzibalqa wrote:
> > 
> > > To be absolutely sure I understand. If I want to default
> > > a function optional argument if it is not used, the command
> > > to that that is
> > > 
> > > (defun dothis x ()
> > > (or x (setq x 8)) ; [...]
> > > (do-something x)
> > > )
> > > 
> > > And this would always work. In will work if I use the
> > > lexical-binding directive at the top of a file. And it would
> > > work if I use dynamic-binding directive at the top of
> > > a file.
> > 
> > Correct, as that wouldn't affect them because function formal
> > parameters (the argument placeholders) are always under
> > lexical/static scope.
> > 
> > > Calling the command (dothis) will always use x with value 8,
> > > and executing (do-something 8) .
> > 
> > Yes, you forgot about the &optional keyword in your example
> > but other than that it is correct.
> 
> 
> Right, it should have been (defun dothis (&optional x)

What had confused me initially was that descriptions customarily 
specified the difference in results between lexical and dynamic
binding.

What I did not immediately realise was that within the context of
function arguments, lexical binding is the default way to handle 
function arguments in Lisp and Emacs Lisp, notwithstanding the file
binding specification.  

Although one has to say that it is also possible to implement 
dynamic binding for function arguments in other languages.  Is 
it possible to impose dynamic binding for function arguments 
in Emacs Lisp though? 

Looks as if dynamic binding in Emacs Lips was affecting global 
variables.  And perhaps function arguments such as lists.




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 19:53                 ` uzibalqa
@ 2023-08-02 23:24                   ` Emanuel Berg
  2023-08-03 15:25                   ` Emanuel Berg
                                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2023-08-02 23:24 UTC (permalink / raw)
  To: help-gnu-emacs

uzibalqa wrote:

> What had confused me initially was that descriptions
> customarily specified the difference in results between
> lexical and dynamic binding.

It is easier to understand it on a case-by-case basis, it is
`let' and `let*' and some other stuff as well - right now,
I don't remember ... ? but it wasn't a huge thing - that can
go either way based on `lexical-binding', the rest don't,
including this case (function argument variables) which is
always lexical/static.

> What I did not immediately realise was that within the
> context of function arguments, lexical binding is the
> default way to handle function arguments in Lisp and Emacs
> Lisp, notwithstanding the file binding specification.
>
> Although one has to say that it is also possible to
> implement dynamic binding for function arguments in other
> languages. Is it possible to impose dynamic binding for
> function arguments in Emacs Lisp though?

You can write your own function-defining function, maybe.

But no, you can't do that in an easy way that I'm aware
of anyway.

And why would you want that? I don't see it makes a lot of
sense. For example, the familiar f(x) = y ... what does that
mean, if x and y are defined outside of that? But should still
influence it? DNC

> Looks as if dynamic binding in Emacs Lips was affecting
> global variables.

It affects how `let' and `let*' behaves, there is a dynamic
let and and a lexical let, and which you get is determined by
the variable you mentioned.

Note that the lexical let, in the presence of
a global/special/dynamic variable with the same name, does not
override it and make a new one that is lexical, no, it will
still be same global and dynamic variable, it just sets
(changes) its value for everything that uses it, within
the let.

So only variables that don't exist get to be either dynamic
or lexical, depending on that setting - and again, we are
just talking `let' and `let*' here, it isn't anything more
than that, sorry :)

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 19:53                 ` uzibalqa
  2023-08-02 23:24                   ` Emanuel Berg
@ 2023-08-03 15:25                   ` Emanuel Berg
  2023-08-03 15:35                   ` Emanuel Berg
  2023-08-03 15:46                   ` Emanuel Berg
  3 siblings, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2023-08-03 15:25 UTC (permalink / raw)
  To: help-gnu-emacs

uzibalqa wrote:

> What I did not immediately realise was that within the
> context of function arguments, lexical binding is the
> default way to handle function arguments in Lisp and Emacs
> Lisp, notwithstanding the file binding specification.

Here, take a look at this code.

First the scope is set to lexical which you should always do,
but here it doesn't influence the code.

Then a global/special/dynamic variable is created with
`defvar'.

Then a lexical argument with the same name is created, and
that will be the boss for every mention in that function.

If you byte-compile it, you will get a warning, but that is
just to avoid confusion, the code will be more clear if the
argument is named something else.

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

;;; -*- lexical-binding: t -*-

(defvar gsd 1)

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

;; (test-gsd)
;; (test-gsd 3)

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 19:53                 ` uzibalqa
  2023-08-02 23:24                   ` Emanuel Berg
  2023-08-03 15:25                   ` Emanuel Berg
@ 2023-08-03 15:35                   ` Emanuel Berg
  2023-08-03 15:46                   ` Emanuel Berg
  3 siblings, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2023-08-03 15:35 UTC (permalink / raw)
  To: help-gnu-emacs

uzibalqa wrote:

> What I did not immediately realise was that within the
> context of function arguments, lexical binding is the
> default way to handle function arguments in Lisp and Emacs
> Lisp, notwithstanding the file binding specification.
>
> Although one has to say that it is also possible to
> implement dynamic binding for function arguments in other
> languages. Is it possible to impose dynamic binding for
> function arguments in Emacs Lisp though?

It seems I was incorrect in this case, argument can be dynamic
like this:

;;; -*- 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




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Passing optional arguments for use with internal functions
  2023-08-02 19:53                 ` uzibalqa
                                     ` (2 preceding siblings ...)
  2023-08-03 15:35                   ` Emanuel Berg
@ 2023-08-03 15:46                   ` Emanuel Berg
  3 siblings, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2023-08-03 15:46 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2023-08-03 15:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-27 23:45 Passing optional arguments for use with internal functions uzibalqa
2023-07-28 10:51 ` Emanuel Berg
2023-07-28 16:42   ` Heime
2023-07-28 17:11     ` Emanuel Berg
2023-07-30 16:09       ` uzibalqa
2023-07-31 12:27         ` Emanuel Berg
2023-08-02 18:01           ` uzibalqa
2023-08-02 18:20             ` Emanuel Berg
2023-08-02 19:24               ` uzibalqa
2023-08-02 19:53                 ` uzibalqa
2023-08-02 23:24                   ` Emanuel Berg
2023-08-03 15:25                   ` Emanuel Berg
2023-08-03 15:35                   ` Emanuel Berg
2023-08-03 15:46                   ` Emanuel Berg
2023-07-28 20:02   ` [External] : " Drew Adams
2023-07-28 20:42     ` uzibalqa

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.