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: docs/src/modules/1-getting-started/3-git.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ Make sure that the branch is named appropriately. For example, if you were creat
38
38
39
39
## Procedure for Pull Requests
40
40
41
-
When you're ready for your work to be incorporated into the `master` branch, open a pull request with the modules repository to merge your
41
+
When you're ready for your work to be incorporated into the `master` branch, [open](https://github.com/source-academy/modules/compare) a pull request with the modules repository to merge your
42
42
changes into `master` branch. Make sure that your branch is up-to-date with the `master` branch (by performing a `git merge` from the `master` branch to your own branch).
43
43
44
44
This will automatically trigger the repository's workflows, which will verify that your changes don't break any of the existing code. If the
@@ -57,4 +57,5 @@ the `dist` folders are ignored for all bundles and tabs, but if you need to add
57
57
directory.
58
58
59
59
> [!TIP]
60
-
> If you want to remove a file that been committed in the past you can use `git rm`
60
+
> If you want to remove a file that been committed in the past you can use `git rm` or\
Copy file name to clipboardExpand all lines: docs/src/modules/2-bundle/4-conventions/1-basic.md
+54-18Lines changed: 54 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ export function exposed_function() {
27
27
Neither default nor rest parameters are currently supported due to an [issue](https://github.com/source-academy/js-slang/issues/1238) on the `js-slang` side.
28
28
:::
29
29
30
-
## 2. Cadet facing functions should not use array destructuring for parameters
30
+
## 2. Cadet facing functions should not use destructuring for parameters
31
31
32
32
Javascript allows us to destruct iterables directly within a function's parameters:
33
33
@@ -48,36 +48,72 @@ function foo([x, y]) {
48
48
TypeError: number 0 is not iterable (cannot read property Symbol(Symbol.iterator))
49
49
```
50
50
51
-
If instead the parameter isn't destructured, it gives you the chance to perform type checking:
52
-
51
+
Javascript also supports object destructuring in parameters:
53
52
```ts
54
-
export function foo(arr: [string, string]) {
55
-
if (!Array.isArray(arr)) throw new Error();
56
-
return arr[0];
53
+
interface BarParams {
54
+
x: string;
55
+
y: string;
57
56
}
58
-
```
59
57
60
-
More information about throwing errors and why this kind of type checking is important can be found [here](./3-errors#source-type-checking).
61
-
62
-
::: details What about Object Destructuring?
63
-
Javascript also supports object destructuring in parameters:
64
-
```js
65
-
function foo({ x, y }) {
58
+
functionbar({ x, y }: BarParams) {
66
59
return x;
67
60
}
68
61
```
69
62
70
-
However, Javascript doesn't actually throw an error if you pass an invalid object into this function:
71
-
```js
72
-
functionfoo({ x, y }) {
63
+
However, Javascript doesn't actually throw an error if you pass an invalid object into the function:
64
+
```ts
65
+
function bar({ x, y }: BarParams) {
73
66
return x;
74
67
}
75
68
76
-
console.log(foo(0)); // prints undefined
69
+
console.log(bar(0)); // prints undefined
77
70
```
78
71
79
72
If an invalid argument gets passed, no error is thrown and the destructured values just take on the value of `undefined` (which you might want to check for).
80
-
:::
73
+
74
+
However, if you use nested destructuring, Javascript _will_ throw an error:
75
+
```ts
76
+
interface Bar2Params {
77
+
x: {
78
+
a: string;
79
+
b: string;
80
+
};
81
+
}
82
+
83
+
function bar2({ x: { a, b } }: Bar2Params) {
84
+
return a;
85
+
}
86
+
87
+
console.log(bar2(0));
88
+
```
89
+
90
+
The call to `bar2` causes an error like the one below:
91
+
```sh
92
+
Uncaught TypeError: Cannot read properties of undefined (reading 'a')
93
+
at bar2 (<anonymous>:1:21)
94
+
at <anonymous>:1:1
95
+
```
96
+
because of course, when `bar2` is called with `0`, `x` becomes `undefined` and trying to destructure `undefined` causes the `TypeError`.
97
+
98
+
If instead the parameter isn't destructured, it gives you the chance to perform type checking:
99
+
100
+
```ts
101
+
exportfunctionfoo(arr: [string, string]) {
102
+
if (!Array.isArray(arr)) throw new Error();
103
+
return arr[0];
104
+
}
105
+
106
+
exportfunctionbar2(obj: Bar2Params) {
107
+
if (typeof obj !== 'object'||!('x'in obj)) {
108
+
// throw an error....
109
+
}
110
+
111
+
return obj.x;
112
+
}
113
+
```
114
+
115
+
> [!IMPORTANT]
116
+
> More information about throwing errors and why this kind of type checking is important can be found [here](./3-errors#source-type-checking).
81
117
82
118
## 3. If your bundle requires specific Source features, make sure to indicate it in the manifest
Copy file name to clipboardExpand all lines: docs/src/modules/2-bundle/4-conventions/2-abstractions.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,6 +95,12 @@ but if your circumstances can't support it you can refer to the second method wh
95
95
> This means that is is unnecessary to implement `ReplResult` for any of your top-level functions. You can still override this automatic functionality by implementing
96
96
> `ReplResult`.
97
97
98
+
> [!INFO]
99
+
> The `ReplResult` interface can be imported from `@sourceacademy/modules-lib/types`. To use it, you will have to
100
+
> add the `@sourceacademy/modules-lib` package to your dependencies.
101
+
>
102
+
> Using the interface as exported will be helpful for type checking, but it is not necessary.
103
+
98
104
### Implementing `ReplResult` directly
99
105
100
106
The simplest way to implement the interface is to do it in Typescript. For example, the `curve` bundle has a `Point` class, which is an abstraction of a point in 3D space with a color value:
0 commit comments