mirror of
https://github.com/Noratrieb/blog.git
synced 2026-01-14 20:35:02 +01:00
restart
This commit is contained in:
parent
fd248a8c1a
commit
eab339d869
8 changed files with 0 additions and 129 deletions
3
404.md
3
404.md
|
|
@ -1,3 +0,0 @@
|
||||||
# i did not find what you asked for
|
|
||||||
|
|
||||||
have you considered learning how to type?
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
# nilstrieb.github.io
|
|
||||||
|
|
||||||
hi
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
theme: jekyll-theme-cayman
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
true
|
|
||||||
|
|
||||||
ref ref ref
|
|
||||||
|
|
||||||
ptr ptr ptr
|
|
||||||
|
|
||||||
ptr ref ptr
|
|
||||||
|
|
||||||
mut
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.3 KiB |
|
|
@ -1,8 +0,0 @@
|
||||||
# cool blog
|
|
||||||
|
|
||||||
yeah it's literally a really cool blog you should totally read it
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
hi
|
|
||||||
* [types](types-rust-ts-java)
|
|
||||||
|
|
@ -1,96 +0,0 @@
|
||||||
# Types in Rust, Typescript and Java
|
|
||||||
|
|
||||||
I find programming languages very interesting, but what I find even more interesting is comparing them
|
|
||||||
and seeing their similarities and differences. So let's do that!
|
|
||||||
|
|
||||||
## Employees
|
|
||||||
|
|
||||||
Say you want to build an employee management program. You could model the employees like this:
|
|
||||||
|
|
||||||
```java
|
|
||||||
public class Employee {
|
|
||||||
public String name;
|
|
||||||
public int number;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> But using public fields in Java is bad practice?
|
|
||||||
|
|
||||||
Go away, I don't care.
|
|
||||||
|
|
||||||
```ts
|
|
||||||
type Employee = {
|
|
||||||
name: string;
|
|
||||||
number: number;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
struct Employee {
|
|
||||||
pub name: String,
|
|
||||||
pub number: i32,
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
They look very similar, don't they? Sure, the syntax differs a bit and Typescript only allows floats (yikes) but they
|
|
||||||
feel like they are the same.
|
|
||||||
|
|
||||||
Now we crate a function to print their names.
|
|
||||||
|
|
||||||
```java
|
|
||||||
public class Employee {
|
|
||||||
public static void printName(Employee employee) {
|
|
||||||
System.out.println(employee.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> Why is the method static???
|
|
||||||
|
|
||||||
Because I decided so.
|
|
||||||
|
|
||||||
```ts
|
|
||||||
const printName = (employee: Employee) => {
|
|
||||||
console.log(employee.name)
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
fn print_name(employee: Employee) {
|
|
||||||
println!("{}", employee.name);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
Still nothing special. Time to call it!
|
|
||||||
|
|
||||||
```java
|
|
||||||
Employee.printName(new Employee("nils", 1));
|
|
||||||
```
|
|
||||||
|
|
||||||
> Assuming we have ~~written~~ auto-generated a constructor
|
|
||||||
|
|
||||||
```ts
|
|
||||||
printName({ name: 'nils', number: 1 })
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
print_name(Employee { name: "nils".to_owned(), number: 1 })
|
|
||||||
```
|
|
||||||
|
|
||||||
> What are you doing with the `to_owned` on the string?
|
|
||||||
|
|
||||||
Rust strings are complicated.
|
|
||||||
|
|
||||||
Hmm, this is interesting. There is one big difference among the three calls: `{ name: 'nils', number: 1 }`.
|
|
||||||
The Typescript code is not saying that its creating an `Employee`, it's just creating some object. What does that mean?
|
|
||||||
|
|
||||||
## Structural Typing
|
|
||||||
|
|
||||||
That's because Typescript uses structural typing. To quote wikipedia:
|
|
||||||
> A structural type system is a major class of type systems in which type compatibility and
|
|
||||||
> equivalence are determined by the type's actual structure or definition and not by
|
|
||||||
> other characteristics such as its name or place of declaration.
|
|
||||||
|
|
||||||
Structural typing means that the type checker only looks at the structure of the types to see whether they match, instead
|
|
||||||
of looking at the names (how Java and Rust do it).
|
|
||||||
9
index.md
9
index.md
|
|
@ -1,9 +0,0 @@
|
||||||
## cool and fun blog
|
|
||||||
|
|
||||||
i love how cool it is
|
|
||||||
|
|
||||||
read [this](cool-blog)
|
|
||||||
|
|
||||||
### Jekyll Themes
|
|
||||||
|
|
||||||
Your Pages site will use the layout and styles from the Jekyll theme you have selected in your [repository settings](https://github.com/Nilstrieb/nilstrieb.github.io/settings/pages). The name of this theme is saved in the Jekyll `_config.yml` configuration file.
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue