Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Scope-Function-names-and-labels/00-META.yaml
Normal file
5
Task/Scope-Function-names-and-labels/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Scope
|
||||
from: http://rosettacode.org/wiki/Scope/Function_names_and_labels
|
||||
note: Basic language learning
|
||||
9
Task/Scope-Function-names-and-labels/00-TASK.txt
Normal file
9
Task/Scope-Function-names-and-labels/00-TASK.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
;Task:
|
||||
|
||||
Explain or demonstrate the levels of visibility of function names and labels within the language.
|
||||
|
||||
|
||||
;See also:
|
||||
* [[Variables]] for levels of scope relating to visibility of program variables
|
||||
* [[Scope modifiers]] for general scope modification facilities
|
||||
<br><br>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
IF PROC x = ...;
|
||||
...
|
||||
THEN
|
||||
# can call x here #
|
||||
PROC y = ...;
|
||||
...
|
||||
GO TO l1 # invalid!! #
|
||||
ELSE
|
||||
# can call x here, but not y #
|
||||
...
|
||||
l1: ...
|
||||
FI
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# This program outputs a greeting
|
||||
BEGIN {
|
||||
sayhello() # Call the function defined below
|
||||
exit
|
||||
}
|
||||
|
||||
function sayhello {
|
||||
print "Hello World!" # Outputs a message to the terminal
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
GOTO 50: REM THIS WILL WORK IMMEDIATELY
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
10 DEF FN S(A)=A*A
|
||||
20 PRINT FN S(2): REM THIS WILL WORK
|
||||
30 PRINT FN C(2): REM CALLING A FUNCTION PRIOR TO DEFINITION MAY NOT WORK
|
||||
40 GOSUB 9000
|
||||
50 PRINT FN C(2): REM THIS WILL WORK
|
||||
60 END
|
||||
9000 DEF FN C(A)=A*A*A
|
||||
9999 RETURN
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
f(1) /* First output line */
|
||||
define f(x) {
|
||||
return(x)
|
||||
}
|
||||
f(3) /* Second output line */
|
||||
|
||||
define f(x) {
|
||||
return(x - 1)
|
||||
}
|
||||
f(3) /* Third output line */
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define sqr(x) ((x) * (x))
|
||||
#define greet printf("Hello There!\n")
|
||||
|
||||
int twice(int x)
|
||||
{
|
||||
return 2 * x;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int x;
|
||||
|
||||
printf("This will demonstrate function and label scopes.\n");
|
||||
printf("All output is happening through printf(), a function declared in the header stdio.h, which is external to this program.\n");
|
||||
printf("Enter a number: ");
|
||||
if (scanf("%d", &x) != 1)
|
||||
return 0;
|
||||
|
||||
switch (x % 2) {
|
||||
default:
|
||||
printf("Case labels in switch statements have scope local to the switch block.\n");
|
||||
case 0:
|
||||
printf("You entered an even number.\n");
|
||||
printf("Its square is %d, which was computed by a macro. It has global scope within the translation unit.\n", sqr(x));
|
||||
break;
|
||||
case 1:
|
||||
printf("You entered an odd number.\n");
|
||||
goto sayhello;
|
||||
jumpin:
|
||||
printf("2 times %d is %d, which was computed by a function defined in this file. It has global scope within the translation unit.\n", x, twice(x));
|
||||
printf("Since you jumped in, you will now be greeted, again!\n");
|
||||
sayhello:
|
||||
greet;
|
||||
if (x == -1)
|
||||
goto scram;
|
||||
break;
|
||||
}
|
||||
|
||||
printf("We now come to goto, it's extremely powerful but it's also prone to misuse. Its use is discouraged and it wasn't even adopted by Java and later languages.\n");
|
||||
|
||||
if (x != -1) {
|
||||
x = -1; /* To break goto infinite loop. */
|
||||
goto jumpin;
|
||||
}
|
||||
|
||||
scram:
|
||||
printf("If you are trying to figure out what happened, you now understand goto.\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
// Test1 is visible to Test2, but not vice versa.
|
||||
// The local variables A, B, and C invisible to the outside world.
|
||||
|
||||
procedure Test1;
|
||||
var A,B,C: integer;
|
||||
begin
|
||||
end;
|
||||
|
||||
procedure Test2;
|
||||
var A,B,C: integer;
|
||||
begin
|
||||
end;
|
||||
|
||||
|
||||
// Test1 is visible to all code that follows it.
|
||||
// Test2 is invisible to the ouside world
|
||||
|
||||
procedure Test1;
|
||||
var A,B,C: integer;
|
||||
|
||||
procedure Test2;
|
||||
var A,B,C: integer;
|
||||
begin
|
||||
end;
|
||||
|
||||
begin
|
||||
end;
|
||||
|
||||
|
||||
// Item1 and Test1 are only visible inside the object
|
||||
// Item2 and Test2 are additional to inhered objects
|
||||
// Item3 and Test3 are visible to the outside world
|
||||
// Item4 is visible to the outside world and the IDE
|
||||
|
||||
|
||||
type TMyObject = class(TObject)
|
||||
private
|
||||
Item1: integer
|
||||
procedure Test1;
|
||||
protected
|
||||
Item2: integer
|
||||
procedure Test2;
|
||||
public
|
||||
Item3: integer
|
||||
procedure Test3;
|
||||
published
|
||||
property Item4: integer read FItem4 write FItem4;
|
||||
end;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
--assume A, B and C to be valid classes
|
||||
class X
|
||||
feature -- alias for "feature {ANY}"
|
||||
-- ANY is the class at the top of the class hierarchy and all classes inherit from it
|
||||
-- features following this clause are given "global" scope: these features are visible to every class
|
||||
|
||||
feature {A, B, C, X}
|
||||
-- features following this clause are only visible to the specified classes (and their descendants)
|
||||
-- classes not in this set do not even know of the existence of these features
|
||||
|
||||
feature {A, B, C}
|
||||
-- similar to above, except other instances of X cannot access these features
|
||||
|
||||
feature {X}
|
||||
-- features following this clause are only visible to instances of X (and its descendants)
|
||||
|
||||
feature {NONE}
|
||||
-- NONE is the class at the bottom of the class hierarchy and inherits from every class
|
||||
-- features following this clause are only visible to this particular instance of X
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
-module( a_module ).
|
||||
|
||||
-export( [exported_function/0] ).
|
||||
|
||||
exported_function() -> 1 + local_function().
|
||||
|
||||
local_function() -> 2.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
USE: math
|
||||
2 2 +
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
USE: io
|
||||
IN: hello-vocab
|
||||
|
||||
hello ! error; hello hasn't been defined yet
|
||||
: hello ( -- ) "Hello, world!" print ;
|
||||
hello ! visible here
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
USE: io
|
||||
IN: hello-vocab
|
||||
|
||||
DEFER: hello
|
||||
hello ! visible here
|
||||
: hello ( -- ) "Hello, world!" print ;
|
||||
hello ! visible here
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"ex"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// func nested() { ... not allowed here
|
||||
|
||||
// this is okay, variable f declared and assigned a function literal.
|
||||
f := func() {
|
||||
// this mess prints the name of the function to show that it's an
|
||||
// anonymous function defined in package main
|
||||
pc, _, _, _ := runtime.Caller(0)
|
||||
fmt.Println(runtime.FuncForPC(pc).Name(), "here!")
|
||||
}
|
||||
|
||||
ex.X(f) // function value passed to exported function
|
||||
|
||||
// ex.x() non-exported function not visible here
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package ex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// X is exported.
|
||||
func X(x func()) {
|
||||
pc, _, _, _ := runtime.Caller(0)
|
||||
fmt.Println(runtime.FuncForPC(pc).Name(), "calling argument x...")
|
||||
x()
|
||||
}
|
||||
|
||||
func x() { // not exported, x not upper case.
|
||||
panic("top level x")
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// labels loop and y both in scope of main
|
||||
loop:
|
||||
for false {
|
||||
continue loop
|
||||
}
|
||||
goto y
|
||||
y:
|
||||
y := 0 // variable namespace is separate from label namespace
|
||||
|
||||
func() {
|
||||
// goto loop ...loop not visible from this literal
|
||||
|
||||
// label y in outer scope not visible so it's okay to define a label y
|
||||
// here too.
|
||||
y:
|
||||
for {
|
||||
break y
|
||||
}
|
||||
y++ // regular lexical scoping applies to variables.
|
||||
}()
|
||||
fmt.Println(y)
|
||||
}
|
||||
|
||||
// end: // labels not allowed outside function blocks
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
add3 :: Int -> Int-> Int-> Int
|
||||
add3 x y z = add2 x y + z
|
||||
|
||||
add2 :: Int -> Int -> Int
|
||||
add2 x y = x + y
|
||||
|
||||
main :: putStrLn(show (add3 5 6 5))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
getSquaredSum :: Int-> Int-> Int
|
||||
getSquaredSum x y = g x + h y
|
||||
where
|
||||
g a = a*a
|
||||
h b = b*b
|
||||
|
|
@ -0,0 +1 @@
|
|||
a=. 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
b=: 2
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
c_thingy_=: 3
|
||||
d=: <'test'
|
||||
e__d=: 4
|
||||
b + e_test_
|
||||
6
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
verb define ''
|
||||
f=. 6
|
||||
g=: 7
|
||||
g=. 8
|
||||
g=: 9
|
||||
)
|
||||
|domain error
|
||||
| g =:9
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
example=:3 :0
|
||||
if. y do.
|
||||
echo 0
|
||||
goto_a.
|
||||
echo 1
|
||||
else.
|
||||
echo 2
|
||||
goto_a.
|
||||
echo 3
|
||||
end.
|
||||
echo 4
|
||||
label_a.
|
||||
echo 5
|
||||
)
|
||||
example 1
|
||||
0
|
||||
5
|
||||
example 0
|
||||
2
|
||||
5
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
def NAME:
|
||||
def NAME: 2;
|
||||
1, NAME; # this calls the inner function, not the outer function
|
||||
|
||||
NAME # => 1, 2
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def F(x): if x == 0 then M(x) else 1 end; # NOT POSSIBLE
|
||||
def M(x): if x == 1 then F(x) else 2 end;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
def F(x):
|
||||
def M(x): if x == 1 then F(x) else 2 end;
|
||||
if x == 0 then M(x) else 1 end;
|
||||
|
||||
def M(x): if x == 1 then F(x) else 2 end;
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
// version 1.1.2
|
||||
|
||||
// top level function visible anywhere within the current module
|
||||
internal fun a() = println("calling a")
|
||||
|
||||
object B {
|
||||
// object level function visible everywhere, by default
|
||||
fun f() = println("calling f")
|
||||
}
|
||||
|
||||
open class C {
|
||||
// class level function visible everywhere, by default
|
||||
fun g() = println("calling g")
|
||||
|
||||
// class level function only visible within C
|
||||
private fun h() = println("calling h")
|
||||
|
||||
// class level function only visible within C and its subclasses
|
||||
protected fun i() {
|
||||
println("calling i")
|
||||
println("calling h") // OK as h within same class
|
||||
// nested function in scope until end of i
|
||||
fun j() = println("calling j")
|
||||
j()
|
||||
}
|
||||
}
|
||||
|
||||
class D : C(), E {
|
||||
// class level function visible anywhere within the same module
|
||||
fun k() {
|
||||
println("calling k")
|
||||
i() // OK as C.i is protected
|
||||
m() // OK as E.m is public and has a body
|
||||
}
|
||||
}
|
||||
|
||||
interface E {
|
||||
fun m() {
|
||||
println("calling m")
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
a() // OK as a is internal
|
||||
B.f() // OK as f is public
|
||||
val c = C()
|
||||
c.g() // OK as g is public but can't call h or i via c
|
||||
val d = D()
|
||||
d.k() // OK as k is public
|
||||
// labelled lambda expression assigned to variable 'l'
|
||||
val l = lambda@ { ->
|
||||
outer@ for (i in 1..3) {
|
||||
for (j in 1..3) {
|
||||
if (i == 3) break@outer // jumps out of outer loop
|
||||
if (j == 2) continue@outer // continues with next iteration of outer loop
|
||||
println ("i = $i, j = $j")
|
||||
}
|
||||
if (i > 1) println ("i = $i") // never executed
|
||||
}
|
||||
val n = 1
|
||||
if (n == 1) return@lambda // returns from lambda
|
||||
println("n = $n") // never executed
|
||||
}
|
||||
l() // invokes lambda
|
||||
println("Good-bye!") // will be executed
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function foo() print("global") end -- global scope by default
|
||||
foo()
|
||||
local function foo() print("local module") end -- local to the current block (which is the module)
|
||||
foo() -- local obscures the global
|
||||
_G.foo() -- bug global still exists
|
||||
do -- create a new block
|
||||
foo() -- outer module-level scope still visible
|
||||
local function foo() print("local block") end
|
||||
foo() -- obscures outer module-level local
|
||||
local function foo() -- redefine at local block level
|
||||
print("local block redef")
|
||||
local function foo() -- define again inside redef
|
||||
print("local block redef inner")
|
||||
end
|
||||
foo() -- call block-level redef inner
|
||||
end
|
||||
foo() -- call block-level redef
|
||||
end -- close the block (and thus its scope)
|
||||
foo() -- module-level local still exists
|
||||
_G.foo() -- global still exists
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
Function Master {
|
||||
Module Alfa {
|
||||
Gosub 100
|
||||
Global M=1000
|
||||
\\ delta print 1000
|
||||
delta
|
||||
End
|
||||
100 Print Module(Beta)=False
|
||||
Print Module(Delta)=True
|
||||
Return
|
||||
}
|
||||
Group Object1 {
|
||||
Function Master {
|
||||
=M
|
||||
}
|
||||
Module Final Beta {
|
||||
\\ delta print 500
|
||||
delta
|
||||
alfa()
|
||||
Sub alfa()
|
||||
Local N=@Kappa(3)
|
||||
Global M=N
|
||||
\\ delta print 1500
|
||||
Delta
|
||||
Print This.Master()=1500
|
||||
N=@Kappa(6)
|
||||
\\ change value of M, not shadow M like Global M
|
||||
M<=N
|
||||
\\ delta print 9000
|
||||
Delta
|
||||
Print .Master()=9000
|
||||
End Sub
|
||||
Function Kappa(K)
|
||||
=M*K
|
||||
End Function
|
||||
}
|
||||
}
|
||||
Module Global Delta {
|
||||
Goto name1
|
||||
\\ a remark here
|
||||
|
||||
name1:
|
||||
Print Module(Alfa)=False
|
||||
Print Module(Beta)=False
|
||||
Print Module(Delta)=True
|
||||
Print M
|
||||
}
|
||||
|
||||
\\ This is the program
|
||||
K=100
|
||||
Global M=500
|
||||
Alfa
|
||||
Object1.Beta
|
||||
Print Object1.Master()=500
|
||||
Print K=100, M=500
|
||||
}
|
||||
Call Master()
|
||||
\\ No variables exist after the return from Master()
|
||||
Print Valid(M)=False
|
||||
|
|
@ -0,0 +1 @@
|
|||
const C = block useless: 3
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Functions are normally internal to a program. If they are at the nesting level
|
||||
immediately within the program, they are accessible from anywhere in the program.
|
||||
|
||||
Functions can also be encapsuled in a package, and the function name exported.
|
||||
|
||||
Functions can be compiled separately, and then linked with a program
|
||||
in which case they are globally accessible.
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
no warnings 'redefine';
|
||||
|
||||
sub logger { print shift . ": Dicitur clamantis in deserto." }; # discarded
|
||||
|
||||
logger('A'); # can use before defined
|
||||
HighLander::logger('B'); # ditto, but referring to another package
|
||||
|
||||
package HighLander {
|
||||
logger('C');
|
||||
sub logger { print shift . ": I have something to say.\n" }; # discarded
|
||||
sub down_one_level {
|
||||
sub logger { print shift . ": I am a man, not a fish.\n" }; # discarded
|
||||
sub down_two_levels {
|
||||
sub logger { print shift . ": There can be only one!\n" }; # routine for 'Highlander' package
|
||||
}
|
||||
}
|
||||
logger('D');
|
||||
}
|
||||
|
||||
logger('E');
|
||||
sub logger {
|
||||
print shift . ": This thought intentionally left blank.\n" # routine for 'main' package
|
||||
};
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function global:Get-DependentService
|
||||
{
|
||||
Get-Service | Where-Object {$_.DependentServices}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
Get-Help about_Scopes
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
/*REXX program demonstrates the use of labels and also a CALL statement. */
|
||||
blarney = -0 /*just a blarney & balderdash statement*/
|
||||
signal do_add /*transfer program control to a label.*/
|
||||
ttt = sinD(30) /*this REXX statement is never executed*/
|
||||
/* [↓] Note the case doesn't matter. */
|
||||
DO_Add: /*coming here from the SIGNAL statement*/
|
||||
|
||||
say 'calling the sub: add.2.args'
|
||||
call add.2.args 1, 7 /*pass two arguments: 1 and a 7 */
|
||||
say 'sum =' result /*display the result from the function.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
add.2.args: procedure; parse arg x,y; return x+y /*first come, first served ···*/
|
||||
add.2.args: say 'Whoa Nelly!! Has the universe run amok?' /*didactic, but never executed*/
|
||||
add.2.args: return arg(1) + arg(2) /*concise, " " " */
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(define (foo x)
|
||||
(define (bar y) (+ x y))
|
||||
(bar 2))
|
||||
(foo 1) ; => 3
|
||||
(bar 1) ; => error
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(define (foo x)
|
||||
(define (bar y) (+ x y))
|
||||
bar)
|
||||
(foo 1) ; => #<procedure:bar>
|
||||
((foo 1) 2) ; => 3
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
# call a routine before it has been defined
|
||||
say log(); # prints: outer
|
||||
|
||||
# define a subroutine that overrides a CORE function
|
||||
sub log { 'outer' };
|
||||
{
|
||||
# redefine the subroutine in this block
|
||||
sub log { 'inner' };
|
||||
{
|
||||
# redefine the subroutine yet again
|
||||
sub log { 'way down inside' };
|
||||
|
||||
# call it within this block
|
||||
say log(); # prints: way down inside
|
||||
|
||||
# call it from the block one level out
|
||||
say &OUTER::log(); # prints: inner
|
||||
|
||||
# call it from the block two levels out
|
||||
say &OUTER::OUTER::log(); # prints: outer
|
||||
|
||||
# call it from the outermost block
|
||||
say &UNIT::log(); # prints: outer
|
||||
|
||||
# call a subroutine that is post declared in outermost scope
|
||||
outersub()
|
||||
}
|
||||
|
||||
{
|
||||
# subroutine in an inner block that doesn't redefine it
|
||||
# uses definition from nearest enclosing block
|
||||
say log(); # prints: inner
|
||||
}
|
||||
# call it within this block
|
||||
say log(); # prints: inner
|
||||
|
||||
# call it from the block one level out
|
||||
say &OUTER::log(); # prints: outer
|
||||
}
|
||||
|
||||
sub outersub{
|
||||
# call subroutine within this block - gets outer sub
|
||||
say log(); # prints: outer
|
||||
|
||||
# call subroutine from the scope of the callers block
|
||||
say &CALLER::log(); # prints: way down inside
|
||||
|
||||
# call subroutine from the outer scope of the callers block
|
||||
say &CALLER::OUTER::log(); # prints: inner
|
||||
|
||||
# call the original overridden CORE routine
|
||||
say &CORE::log(e); # prints: 1 ( natural log of e )
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Project : Scope/Function names and labels
|
||||
|
||||
see "What is your name?" + nl
|
||||
give name
|
||||
welcome(name)
|
||||
|
||||
func welcome(name)
|
||||
see "hello " + name + nl
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def welcome(name)
|
||||
puts "hello #{name}"
|
||||
end
|
||||
puts "What is your name?"
|
||||
$name = STDIN.gets
|
||||
welcome($name)
|
||||
return
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
object ScopeFunction extends App {
|
||||
val c = new C()
|
||||
val d = new D()
|
||||
val n = 1
|
||||
|
||||
def a() = println("calling a")
|
||||
|
||||
trait E {
|
||||
def m() = println("calling m")
|
||||
}
|
||||
|
||||
a() // OK as a is internal
|
||||
B.f() // OK as f is public
|
||||
|
||||
class C {
|
||||
// class level function visible everywhere, by default
|
||||
def g() = println("calling g")
|
||||
|
||||
// class level function only visible within C and its subclasses
|
||||
protected def i() {
|
||||
println("calling i")
|
||||
println("calling h") // OK as h within same class
|
||||
// nested function in scope until end of i
|
||||
def j() = println("calling j")
|
||||
|
||||
j()
|
||||
}
|
||||
|
||||
// class level function only visible within C
|
||||
private def h() = println("calling h")
|
||||
}
|
||||
|
||||
c.g() // OK as g is public but can't call h or i via c
|
||||
|
||||
class D extends C with E {
|
||||
// class level function visible anywhere within the same module
|
||||
def k() {
|
||||
println("calling k")
|
||||
i() // OK as C.i is protected
|
||||
m() // OK as E.m is public and has a body
|
||||
}
|
||||
}
|
||||
|
||||
d.k() // OK as k is public
|
||||
|
||||
object B {
|
||||
// object level function visible everywhere, by default
|
||||
def f() = println("calling f")
|
||||
}
|
||||
|
||||
val l = (i:Int, j: Int) => println(i,j)
|
||||
|
||||
println("Good-bye!") // will be executed
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# Nested functions
|
||||
func outer {
|
||||
func inner {}; # not visible outside
|
||||
}
|
||||
|
||||
# Nested classes
|
||||
class Outer {
|
||||
class Inner {}; # not visisble outside
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
doFoo 1 2 3; # Will produce an error
|
||||
|
||||
proc doFoo {a b c} {
|
||||
puts [expr {$a + $b*$c}]
|
||||
}
|
||||
doFoo 1 2 3; # Will now print 7 (and will continue to do so until doFoo is renamed or deleted
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#!/bin/sh
|
||||
multiply 3 4 # This will not work
|
||||
echo $? # A bogus value was returned because multiply definition has not yet been run.
|
||||
|
||||
multiply() {
|
||||
return `expr $1 \* $2` # The backslash is required to suppress interpolation
|
||||
}
|
||||
|
||||
multiply 3 4 # Ok. It works now.
|
||||
echo $? # This gives 12
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
fn world() {
|
||||
print("World!")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
// anonymous function
|
||||
f := fn() {
|
||||
print("Hello ")
|
||||
}
|
||||
f() // "Hello
|
||||
world() // World!"
|
||||
|
||||
// "Hello World!"
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fn main() {
|
||||
println(add(77, 33))
|
||||
println(sub(100, 50))
|
||||
}
|
||||
|
||||
fn add(x int, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
fn sub(x int, y int) int {
|
||||
return x - y
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
pub fn public_function() {
|
||||
}
|
||||
|
||||
fn private_function() {
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
outer: for i := 4; true; i++ {
|
||||
println(i)
|
||||
for {
|
||||
if i < 7 {
|
||||
continue outer
|
||||
} else {
|
||||
break outer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
// Unsafe 'goto' pseudo example:
|
||||
if x {
|
||||
// ...
|
||||
if y {
|
||||
unsafe {
|
||||
goto my_label
|
||||
}
|
||||
}
|
||||
// ...
|
||||
}
|
||||
my_label:
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
//func.call() /* invalid, can't call func before its declared */
|
||||
|
||||
var func = Fn.new { System.print("func has been called.") }
|
||||
|
||||
func.call() // fine
|
||||
|
||||
//C.init() /* not OK, as C is null at this point */
|
||||
|
||||
class C {
|
||||
static init() { method() } // fine even though 'method' not yet declared
|
||||
static method() { System.print("method has been called.") }
|
||||
}
|
||||
|
||||
C.init() // fine
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
9000 REM The function is immediately visible and usable
|
||||
9010 DEF FN s(x)=x*x
|
||||
|
||||
PRINT FN s(5): REM This will work immediately
|
||||
GO TO 50: REM This will work immediately
|
||||
|
|
@ -0,0 +1 @@
|
|||
class C{ fcn [private] f{} }
|
||||
Loading…
Add table
Add a link
Reference in a new issue