Topic variable: Difference between revisions

From Rosetta Code
Content added Content deleted
m (utilization and behaviour draft wording)
(reformulation)
Line 4: Line 4:
Demonstrate the utilization and behaviour of the topic variable within the language and explain or demonstrate how the topic variable behaves under different levels of nesting or scope, if this applies, within the language.
Demonstrate the utilization and behaviour of the topic variable within the language and explain or demonstrate how the topic variable behaves under different levels of nesting or scope, if this applies, within the language.


If your language has something similar, show how it can be used by affecting <math>3</math> to the topic variable, and then compute its square and square root.
For instance you can (but you don't have to) show how the topic variable can be used by affecting the number <math>3</math> to it, and then compute its square and square root.


=={{header|J}}==
=={{header|J}}==

Revision as of 14:38, 28 January 2013

Topic variable is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted.

Demonstrate the utilization and behaviour of the topic variable within the language and explain or demonstrate how the topic variable behaves under different levels of nesting or scope, if this applies, within the language.

For instance you can (but you don't have to) show how the topic variable can be used by affecting the number to it, and then compute its square and square root.

J

With this new definition of topic variables, the closest thing J has to a topic variable is probably dummy variables used in function definitions, because we always omit declaring which variables they are, and because we may omit them entirely. But, we still need to place the function in the context of the value it's being used on.

Thus, for example:

<lang J> example=: *:, %: NB. *: is square, %: is square root

  example 3

9 1.73205</lang>

Or course, if it's crucial to the concept of topic variables that they not be constrained to definitions of things like functions, then it might be argued that J does not have them.

Perl

In Perl the topic variable is $_. It is the default argument for many functions, including the square root. It is also the default parameter for loops. The topic parameter is lexical, so its use can be nested into several lexical scopes.

<lang Perl>my $_ = 3; print for $_**2, "\n", sqrt;</lang>

Perl 6

As in previous versions of Perl, in Perl6 the topic variable is $_. In addition to a direct affectation, it can also be set with the 'given' keyword. A method can be called from it with an implicit call:

<lang Perl 6>given 3 {

   .say for $_**2, .sqrt;

}</lang>

Standard ML

If an SML expression is evaluated interactively (that is, the fragment of program provided interactively is an expression and not a variable initialization), the value of the expression is used to initialize a variable, it. This is a feature of the interactive shell only.

This example is incomplete. Actually need to do the details of the task, and not just talk about it. Please ensure that it meets all task requirements and remove this message.