zsh-users
 help / color / mirror / code / Atom feed
* Function or Alias - Does it matter?
@ 2014-11-19  0:13 TJ Luoma
  2014-11-19  0:45 ` James Pearson
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: TJ Luoma @ 2014-11-19  0:13 UTC (permalink / raw)
  To: Zsh-Users List

Is there any practical difference between making an alias or a
function if they do the same thing?

For example, these are (as far as I know) functionally identical:

         function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p"
"$EPOCHSECONDS" }

         alias timestamp_r='strftime "%Y/%m/%d at %-I:%M:%S %p" "$EPOCHSECONDS"'

Is there any reason to prefer one or the other?

Tj


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

* Re: Function or Alias - Does it matter?
  2014-11-19  0:13 Function or Alias - Does it matter? TJ Luoma
@ 2014-11-19  0:45 ` James Pearson
  2014-11-19  2:51 ` Bart Schaefer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: James Pearson @ 2014-11-19  0:45 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 663 bytes --]

Functions can take arguments, which makes them more powerful for future
expansion. I forget if functions and aliases are treated the same by which
(as I recall zsh expands both of them).
On Nov 18, 2014 4:15 PM, "TJ Luoma" <luomat@gmail.com> wrote:

> Is there any practical difference between making an alias or a
> function if they do the same thing?
>
> For example, these are (as far as I know) functionally identical:
>
>          function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p"
> "$EPOCHSECONDS" }
>
>          alias timestamp_r='strftime "%Y/%m/%d at %-I:%M:%S %p"
> "$EPOCHSECONDS"'
>
> Is there any reason to prefer one or the other?
>
> Tj
>

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

* Re: Function or Alias - Does it matter?
  2014-11-19  0:13 Function or Alias - Does it matter? TJ Luoma
  2014-11-19  0:45 ` James Pearson
@ 2014-11-19  2:51 ` Bart Schaefer
  2014-11-19  8:59 ` Oliver Kiddle
  2014-11-23 22:35 ` zzapper
  3 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-11-19  2:51 UTC (permalink / raw)
  To: Zsh-Users List

On Nov 18,  7:13pm, TJ Luoma wrote:
}
} Is there any practical difference between making an alias or a
} function if they do the same thing?

This depends on how precisely you mean "the same thing".

An alias is a textual replacement.  It works like that setting in your
phone SMS app that your friends use to prank you by having "LOL" expand
to a rude phrase if you leave the phone unlocked where they can find it.

A function is a semantic construct that encapsulates a series of other
semantic constructs so you can easily/repeatedly execute that series,
even if it's a series of only one item.

} Is there any reason to prefer one or the other?

An alias can change the semantics of the construct in which it appears
by changing the text and syntax before parsing is done.  A function
can manage a local environment (parameters, options, etc.) without
altering the environment of its caller.  Which one to prefer depends
on what you want to accomplish.


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

* Re: Function or Alias - Does it matter?
  2014-11-19  0:13 Function or Alias - Does it matter? TJ Luoma
  2014-11-19  0:45 ` James Pearson
  2014-11-19  2:51 ` Bart Schaefer
@ 2014-11-19  8:59 ` Oliver Kiddle
  2014-11-23 22:35 ` zzapper
  3 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2014-11-19  8:59 UTC (permalink / raw)
  To: Zsh-Users List

TJ Luoma wrote:
> function if they do the same thing?
> 
> For example, these are (as far as I know) functionally identical:
> 
>          function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p"
> "$EPOCHSECONDS" }
> 
>          alias timestamp_r='strftime "%Y/%m/%d at %-I:%M:%S %p" "$EPOCHSECONDS"'
> 
> Is there any reason to prefer one or the other?

If you find your function finishes by passing "$@" as the last argument
to whatever command it is, then there's a good chance that an alias will
suffice. Note that (unless you unsetopt completealiases), completion
after an alias will look at the alias definition so with, e.g:
  alias ll='ls -lFb'
  ll -<tab>
this will still complete options to ls. With the function equivalent that
won't work without an explicit compdef.

Functions can be more flexible with their arguments. For example, the
following slight modification to your function makes EPOCHSECONDS
merely a default if you don't specify a different time. This is not
possible with an alias:
  function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p" "${1:-$EPOCHSECONDS}" }

Oliver


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

* Re: Function or Alias - Does it matter?
  2014-11-19  0:13 Function or Alias - Does it matter? TJ Luoma
                   ` (2 preceding siblings ...)
  2014-11-19  8:59 ` Oliver Kiddle
@ 2014-11-23 22:35 ` zzapper
  3 siblings, 0 replies; 5+ messages in thread
From: zzapper @ 2014-11-23 22:35 UTC (permalink / raw)
  To: zsh-users


> 
> Is there any reason to prefer one or the other?
> 
> Tj

My preference would be an alias especially since I can TAB complete it, 
however when I need something a bit more complicated then it's got to be a 
function.

You can comment functions etc



-- 
zzapper
https://twitter.com/dailyzshtip

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com



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

end of thread, other threads:[~2014-11-23 22:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-19  0:13 Function or Alias - Does it matter? TJ Luoma
2014-11-19  0:45 ` James Pearson
2014-11-19  2:51 ` Bart Schaefer
2014-11-19  8:59 ` Oliver Kiddle
2014-11-23 22:35 ` zzapper

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).