Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): update docs #488

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Variable declaration
id: variable-declaration
keywords: [Wing example]
---

### Assignment

```ts
let x = 12;
x = 77; // ERROR: x is non reassignable

let var y = "hello";
y = "world"; // OK (y is reassignable)
```

### Inferred typing
```ts playground
let x1 = 12;
let x2: num = 12; // equivalent
```

### Optionals
```ts playground
let var x1 = "Hello"; // type str, value "Hello"
let var x2: str = "Hello"; // same as above
let var x3: str? = "Hello"; // type str? (optional), value "Hello"
let var x4: str? = nil; // type str? (optional), value nil

x1 = nil; // ERROR: Expected type to be "str", but got "nil" instead
x3 = nil; // OK (x3 is optional)
```
### Scopes
```ts playground
let s = "parent";
log(s); // prints parent
if true {
let s = "inner";
log(s); // prints inner
}
log(s); // prints parent
```
78 changes: 78 additions & 0 deletions versioned_docs/version-latest/07-examples/02-primitives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Primitives
id: primitives
keywords: [Wing example]
---


## Str
### Concat and Interpolate
```ts playground
let s1 = "Hello Wing String";
log(s1); // prints Hello Wing String
let s2 = "Interpolate: ${s1}"; // string interpolation
log(s2); // prints Interpolate Hello Wing String
let s3 = "Concat: " + s1; // string concatenation
log(s3); // prints Concat: Hello Wing String
```

### Str methods
```ts playground
let s = "Hello to a new Wing world";

// lets start with split
for w in s.split(" ") {
if w.startsWith("H") {
log(w); // 'Hello' starts with H
}
if w.length > 3 && w.lowercase() == w {
log(w); // 'world' is lowercased with more then 3 chars
}
if w.contains("in") && w.endsWith("g") {
log(w); // 'Wing' has in and end with g
}
if s.indexOf(w) == 6 {
log(w); // 'to' position is 6
}
if s.at(9) == w {
log(w); // 'a' is a single char
}
if s.substring(11,14) == w {
log(w); // 'new' position is 11-14
}
}
```

## Num

```ts playground
let n1 = 10;
log("${n1}"); // 10

let n2 = n1 - 10 / 10 + 1 * 10 ; // arithmetic
log("${n2}"); // 1

let n3 = 10 % 7; // modulo
log("${n3}"); // 3

let n4 = 10 \ 7; // FloorDiv
log("${n4}"); // 1

let n5 = 2 ** 10; // power of
log("${n5}"); // 1024

let n6 = (10 + 1) / (12 - 1) + (20 / 2) * 2 + 10 * 2**10 / 512 + 1;
log("${n6}"); // The meaning of the universe
```


## Bool

```ts playground
let b1 = true;
let b2 = false;

if b1 && !b2 {
log("${b1},${b2}"); // prints true, false
}
```
51 changes: 51 additions & 0 deletions versioned_docs/version-latest/07-examples/03-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Functions
id: functions-example
keywords: [Wing example]
---


### Preflight function

```ts playground
// preflight function - when declared in preflight context
let dup = (s: str, count: num): str => {
// code
};
```

### Inflight functions

Inflight functions are Wing's distributed computing primitive. They are isolated code blocks which can be packaged and executed on compute platforms in the cloud (such as containers, Lambda/Cloud Function, etc..).

```ts playground

let handler = inflight (message: str): void => {
// using the inflight modifier
let dup = inflight (s: str, count: num): str => {
// code
};
// inflight modifier is not required when function is declared in inflight context
let dup = (s: str, count: num): str => {
// code
};
};
```
### Struct Expansion
```ts playground
struct Options {
prefix: str?;
delim: str;
}

let join_str = (a: Array<str>, opts: Options):str => {
let prefix = opts.prefix ?? "";
return prefix + a.join(opts.delim);
};

log(join_str(["hello", "world"], delim: ", ")); // "!hello.world"

// also OK to pass an object
let opts = Options { delim: "/" , prefix: "!!" };
log(join_str(["hello", "world"], opts)); // "!!hello/world");
```
97 changes: 97 additions & 0 deletions versioned_docs/version-latest/07-examples/04-flow-controls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
title: Flow controls
id: flow-controls
keywords: [Wing example]
---

### For in

```ts playground
let iterable = ["a", "b", "c", "d", "e", "f", "g", "h"];
for value in iterable {
if value == "g" {
// stoping at g
break;
}
if value == "b" {
// skipping b
continue;
}
log(value);
}
/**
* prints
a
c
d
e
f
**/
```

### For through a range

```ts playground
// print numbers from 0 to 9
for value in 0..10 {
log("${value}");
}
```

### If elif else

```ts playground
let grade = (score: num): str => {
// Parentheses are optional in conditions.
// However, curly braces are required in `if/else` statements.
if 0 < score && score < 55 {
return "F";
} elif 55 <= score && score < 65 {
return "C";
} elif 65 <= score && score < 75 {
return "B";
} elif 75 <= score && score <= 100 {
return "A";
} else {
return "Invalid grade";
}
};

log("54 is ${grade(54)}"); // 54 is F
log("62 is ${grade(62)}"); // 62 is C
log("68 is ${grade(68)}"); // 68 is B
log("99 is ${grade(99)}"); // 99 is A
log("101 is ${grade(101)}"); // 101 is Invalid grade
```

### While

```ts playground
let var i = 0;
while i < 100 {
i = i + 1;
if i == 20 {
// although the while loop goes to 100, we break it at 20
break;
}
if i % 2 == 0 {
// continue for even numbers
continue;
}
log("${i}");
}
/**
* prints
1
3
5
7
9
11
13
15
17
19
**/
```

102 changes: 102 additions & 0 deletions versioned_docs/version-latest/07-examples/05-optionality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
title: Optionality
id: optionality
keywords: [Wing example]
---

## Definition

```ts playground
let s1: str? = "Hello"; // type str? (optional), value "Hello"
let s2: str? = nil; // type str? (optional), value nil
```

## Testing existence
```ts playground
let s1: str? = "Hello"; // type str? (optional), value "Hello"
let s2: str? = nil; // type str? (optional), value nil

if s1? {
log("x1 is not nil");
}
if !s2? {
log("x2 is nil");
}
```

## Using if let
```ts playground
let s1: str? = "Hello"; // type str? (optional), value "Hello"

// unwrap optional s1 and create s from type str
if let s = s1 {
log("s is not optional, value ${s}");
} else {
log("s1 was nil, s doesn't exists in this scope");
}

// same as above but shadowing s1 variable
if let s1 = s1 {
log("s1 type is str, value ${s1}");
} else {
log("s1 was nil");
}
log("s1 type is optional str");
```

## Using ??

```ts playground
let s1: str? = nil; // type str? (optional), value nil
let s2 = s1 ?? "default value"; // s2 is of type str
log(s2); // prints default value
```

## Optional Chaining

```ts playground
let j = Json {
working: {
a: {
b: "value"
}
},
broken: {}
};

if let value = j.tryGet("working")?.tryGet("a")?.tryGet("b")?.tryAsStr() {
log("value is ${value}");
}

if let value = j.tryGet("broken")?.tryGet("a")?.tryGet("b")?.tryAsStr() {
// not reachable
} else {
log("value was not found");
}
```

## Optional bool

```ts playground
let b3: bool? = false;

if b3? {
log("although b3 is false, the if statement here checks for existence of value");
}

if let b3 = b3 { // unboxing b3 and shadowing original b3
if b3 {
log("b3 is true");
} else {
log("b3 is false");
}
} else {
log("b3 is nil");
}

/**
* prints:
although b3 is false, the if statement here checks for existence of value
b3 is false
**/
```
Loading