Skip to content

Commit f1856dc

Browse files
Update Struct Topic
1 parent 08d6810 commit f1856dc

6 files changed

Lines changed: 79 additions & 6 deletions

File tree

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
```Cs
2-
// TODO
2+
struct Point
3+
{
4+
public int X { get; set; }
5+
public int Y { get; set; }
6+
7+
public Point(int x, int y)
8+
{
9+
X = x;
10+
Y = y;
11+
}
12+
}
13+
14+
Point point = new Point(10, 20);
15+
16+
// Since C# 10
17+
readonly record struct Size(int Width, int Height);
318
```
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
```Go
2-
// TODO
2+
type Point struct {
3+
X int
4+
Y int
5+
}
6+
7+
point := Point{X: 10, Y: 20}
8+
point.X = 30
9+
10+
// Anonymous struct
11+
user := struct {
12+
Name string
13+
Age int
14+
}{
15+
Name: "Ada",
16+
Age: 36,
17+
}
318
```
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
```Java
2-
// TODO
2+
// No Native Struct Support.
3+
// Use a class or record. Records are available since Java 16.
4+
5+
record Point(int x, int y) {}
6+
7+
Point point = new Point(10, 20);
8+
int x = point.x();
39
```
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
```Python
2-
# TODO
2+
from dataclasses import dataclass
3+
4+
@dataclass
5+
class Point:
6+
x: int
7+
y: int
8+
9+
point = Point(10, 20)
10+
11+
# Alternative lightweight immutable tuple-like type
12+
from typing import NamedTuple
13+
14+
class Size(NamedTuple):
15+
width: int
16+
height: int
317
```
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
```Rust
2-
// TODO
2+
struct Point {
3+
x: i32,
4+
y: i32,
5+
}
6+
7+
let point = Point { x: 10, y: 20 };
8+
9+
// Tuple struct
10+
struct Color(u8, u8, u8);
11+
12+
let color = Color(255, 0, 0);
313
```
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
```Typescript
2-
// TODO
2+
// No Native Struct Support.
3+
// Use type aliases or interfaces for object shapes.
4+
5+
type Point = {
6+
x: number;
7+
y: number;
8+
};
9+
10+
const point: Point = { x: 10, y: 20 };
11+
12+
interface Size {
13+
width: number;
14+
height: number;
15+
}
316
```

0 commit comments

Comments
 (0)