Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module tests;
- import cstd.io;
- import std.math;
- import export std.error;
- main:: (argc: int, argv: [][]char) [ret: int] {
- [x, y] := rng(1245);
- val := 3;
- app(*val);
- if (root := sqrt(-22.0); is_type<std.error>(root)) {
- // Invalid sqrt.
- }
- // Can ignore error return.
- root := sqrt(4.0);
- v: float = root + 2.0;
- // Optional return.
- root2 := sqrt2(16.0);
- if (@root2) {
- v2: float = root2 + 4.0;
- }
- fp: FunctionPointer;
- fp.add2 = <T> (a: T, b: T) [ret: T] {
- return a + (2 * b);
- };
- cstd.printf("%i\n", fp.doIt(-10, 2));
- d: D<float64>;
- d.print();
- dog := animals.dog;
- ret = 0;
- }
- rng:: (seed: int) [x: float, y: float] {
- x = (seed % 3 * 22 / 2) * 0.1f;
- y = ((seed << 2) % 2 * 33 / 4) * 0.3f;
- }
- // You don't need to write void in the return args.
- app<T>:: (val: *T) [void] {
- while (true) {
- if (val > 100) {
- // Breaks from the function, val is returned.
- return;
- }
- val *= 2;
- }
- // A type that is also a pointer/optional is used exactly like a normal type,
- // To get the pointer/optional-bool you use the @ prefix-operator.
- cstd.printf("app.val: %p\n", @val);
- val2: T;
- // OK: copys val int val2.
- val2 = val;
- val3: *T;
- // ERROR: T* and T do not implicitly match.
- //val3 = val;
- // OK: copys the pointer address of val into val3.
- val3 = @val;
- val4: **T;
- // OK: Gets a pointer to the val pointer.
- val4 = *@val;
- // ERROR: Cannot assign int32 to T*.
- //val4 = 3;
- // OK: Derefs *T down to T.
- &val4 = 3;
- }
- // Varient return type, only one can be set at a time.
- sqrt<T>:: (val: T) ![ret: T, err: std.error] {
- if (val >= 0) {
- ret = cstd.sqrt(val);
- } else {
- err = std.math_error("Cannot take sqrt of a negative number!");
- }
- }
- sqrt2<T>:: (val: T) [ret: ?T] {
- if (val > 0) {
- ret = cstd.sqrt(val);
- } else {
- ret = nil;
- }
- }
- FunctionPointer:: {
- add2: std.function<(int, int), [int]>;
- doIt:: (a: int, b: int) [ret: int] {
- ret = add2(a, b);
- }
- }
- A:: [
- funA:: (int) [bool];
- ]
- B<T>:: A [
- // funA is does not need a redefinition.
- funB:: (bool) [T];
- ]
- C:: A [
- funC:: () [float];
- ]
- D<K>:: B<K>, C, A [
- private {
- constVal := 4;
- }
- // Compile-time auto type decution for method paramaters and returns.
- getConstVal:: () [ret] { ret = constVal; }
- setConstVal:: (val) [] { constVal = val; }
- funA:: (val: int) [ret: bool] {
- ret = val > 7 && val % 2 != 0;
- }
- funB:: (val: bool) [ret: K] {
- ret = val ? 3.1415 : 9.8282;
- }
- funC:: () [ret: float] {
- ret = constVal * 2.0;
- }
- print:: () [] {
- val: K = funC() * funB(funA());
- cstd.println(cal);
- }
- ]
- enum<T: int32>$$ (unit: *std.struct) [
- assert(unit.methods.empty());
- previousVal := 1;
- // A foreach syntax is needed.
- for (it := unit.members.begin(); it < unit.members.end(); ++it) {
- // A variable/members not having a type is not a syntax error,
- // only fails to compile if not found by end of preprossesor.
- assert(member.auto_type());
- member.set_type<T>();
- if (!member.assigned_val()) {
- member.set_val<T>(++previousVal);
- }
- previousVal = member.val<T>();
- }
- ]
- animals:: enum<int16> [
- dog;
- cat = 3;
- fish;
- bird;
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement