You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/basic.md
+18-3Lines changed: 18 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -463,6 +463,13 @@ Examples:
463
463
function g(a as single, b as string) as string
464
464
...
465
465
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
466
473
```
467
474
468
475
#### Arrays
@@ -1130,9 +1137,7 @@ Evaluates `a`, and if it is true then it evaluates `b` and returns `b`; otherwis
1130
1137
```
1131
1138
dim x as any
1132
1139
```
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.
1136
1141
1137
1142
Example: a subroutine to print the raw bit pattern of a floating point
1138
1143
number:
@@ -1148,6 +1153,16 @@ number:
1148
1153
end sub
1149
1154
```
1150
1155
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
+
1151
1166
### APPEND
1152
1167
1153
1168
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