Skip to content

Commit 747b9a1

Browse files
committed
better documentation for templates
1 parent 486bcab commit 747b9a1

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

doc/basic.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ Examples:
463463
function g(a as single, b as string) as string
464464
...
465465
end function
466+
467+
'' this function takes any type as input and returns the same type as output
468+
'' writing such functions can be tricky, one often has to use `__sametypes`
469+
'' or something similar
470+
any(T) function h(a as T) as T
471+
...
472+
end function
466473
```
467474

468475
#### Arrays
@@ -1130,9 +1137,7 @@ Evaluates `a`, and if it is true then it evaluates `b` and returns `b`; otherwis
11301137
```
11311138
dim x as any
11321139
```
1133-
Declares x as a generic 32 bit variable compatible with any other type. Basically this is a way to treat a variable as a raw 32 bit value. Note that no type checking at all is performed on variables declared with type `any`, nor are any conversions applied to them. This means that the compiler will not be able to catch many common errors.
1134-
1135-
`any` should be used only in exceptional circumstances.
1140+
Declares x as a generic 32 bit variable compatible with any other type. Basically this is a way to treat a variable as a raw 32 bit value. Note that no type checking at all is performed on variables declared with type `any`, nor are any conversions applied to them. This means that the compiler will not be able to catch many common errors. `any` should be used like this only in exceptional circumstances.
11361141

11371142
Example: a subroutine to print the raw bit pattern of a floating point
11381143
number:
@@ -1148,6 +1153,16 @@ number:
11481153
end sub
11491154
```
11501155

1156+
`any` may also be used to introduce a function template which accepts different types, e.g.:
1157+
```
1158+
' function to add any two variables of the same type
1159+
any(T) function do_add(x as T, y as T) as T
1160+
return x+y
1161+
end function
1162+
```
1163+
1164+
This will create functions such as `do_add_integer`, `do_add_string` to handle the different actual uses of the templated function. See the description of templates for more details.
1165+
11511166
### APPEND
11521167

11531168
Reserved word. For now, its only use is in `open` statements to specify that an existing file should be opened in append mode.

0 commit comments

Comments
 (0)