Live data from Hacker News

Moving from TypeScript to Rust / WebAssembly

nicolodavis.com

41–50 of 428 posts

Re: Moving from TypeScript to Rust / WebAssembly

#41
> However, it [Typescript] does not actually ensure that the data you are manipulating corresponds to the type that you have declared to represent it. For example, the data might contain additional fields or even incorrect values for declared types.

This made me curious, as it sounds like something that can be added as a transpiler option under tsconfig.json. But it turns out Typescript doesn't already have a support for "nominal" type, but there are some tricks one can leverage to achieve similar thing:

https://medium.com/better-programming/nominal-typescript-eee...

Re: Moving from TypeScript to Rust / WebAssembly

#42
post #20

As long as you use a language with a good, static type system, I don't care so much what you use. TypeScript or Rust, for all I care you can transpile Haskell into JavaScript if you feel adventurous. But don't use a dynamically typed language as the source language for anything that is supposed to be more than a simple script.

This is silly. There are systems which have had nine nines of uptime written in dynamically typed languages.

That it can be done doesn't mean is the optimal approach.

I was a diehard fan of dynamic languages for a decade, but eventually I saw the light.

I still use dynamic languages daily, but the bigger the project the more I want proper static typing. None of that mypy stuff, the real deal.

Re: Moving from TypeScript to Rust / WebAssembly

#43
post #34

Earlier quoted context omitted.

That's correct. You have to insert validation code at all the entry points, which was the case for me. Moving to Rust doesn't eliminate validation altogether, but you don't have to do any type related validation which is nice.

Not sure if this is a place to ask this but if someone does not have experience working with Javascript, they might have trouble reasoning about this code: https://codesandbox.io/s/is849 edit: complete code ```typescript class Person { id: number; name: string; yearOfBirth: number; constructor(id: number, name: string, yearOfBirth: number) { this.id = id; this.name = name; if (yearOfBirth 2020) { throw new Error("I d…

Unfortunately HN doesn't support commonmark's triple backtick code blocks. You'll need to use 4 spaces before each line of code.

Re: Moving from TypeScript to Rust / WebAssembly

#44
post #16

Earlier quoted context omitted.

> isn’t JavaScript a bit more of a higher level language Rust has zero-cost abstractions that feel like using Ruby and a rich type system that makes it incredibly expressive. Working in other languages feels like going back to assembly. I wouldn't want to design state machines in any other language. Rust's enums make it feel smooth as butter. They're a killer app. (The whole ecosystem is. Cargo. Package naming. Trait…

I'm a big Rust fan too. I have experience in all of the languages you've mentioned, and I'm most productive in Rust as well. It's funny you mention enums, there was just another thread last week where I brought up how crazy it is that sum types (Rust enums) and pattern matching have been around since the 70s but have largely been limited to the FP languages. After extensively using Rust for ~3 years now, I don't ever…

I just implemented an iterator in rust yesterday. After having done it in c++ back in the day, I was blown away by how easy and powerful it is.

Re: Moving from TypeScript to Rust / WebAssembly

#46
> webassembly is faster than javascript

Everyone says this, but I would dispute it as misleading in a lot of cases. I've been experimenting a lot with wasm lately. Yes, it is faster than javascript, but not by all that much.

It's the speed of generic 32 bit C. It leaves a lot to be desired in the way of performance. My crypto library, when compiled to web assembly, is maybe 2-3x the speed of the equivalent javascript code. Keep in mind this library is doing integer arithmetic, and fast integer arithmetic does not explicitly exist in javascript -- JS is at a _huge_ disadvantage here and is still producing comparable numbers to WASM.

This same library is maybe 15 or 16 times faster than JS when compiled natively, as it is able to utilize 128 bit arithmetic, SIMD, inline asm, and so on.

Maybe once WASM implementations are optimized more the situation will be different, but I am completely unimpressed with the speed of WASM at the moment.

Re: Moving from TypeScript to Rust / WebAssembly

#47
post #40
post #34

Earlier quoted context omitted.

Not sure if this is a place to ask this but if someone does not have experience working with Javascript, they might have trouble reasoning about this code: https://codesandbox.io/s/is849 edit: complete code ```typescript class Person { id: number; name: string; yearOfBirth: number; constructor(id: number, name: string, yearOfBirth: number) { this.id = id; this.name = name; if (yearOfBirth 2020) { throw new Error("I d…

What’s confusing? I must’ve missed it in my cursory scan.

Dog and Person are structurally the same so you can assign a person to a dog and vice versa. But that's just how structural typing works and as a user of TypeScript I haven't run into a case where this'd be an issue.

Re: Moving from TypeScript to Rust / WebAssembly

#48
post #40

Earlier quoted context omitted.

What’s confusing? I must’ve missed it in my cursory scan.

Dog and Person are structurally the same so you can assign a person to a dog and vice versa. But that's just how structural typing works and as a user of TypeScript I haven't run into a case where this'd be an issue.

Haha oh. Yeah, assumed that the Dog definition wasn’t worthless. Indeed TS is structurally typed. And that’s nice!

Re: Moving from TypeScript to Rust / WebAssembly

#49
post #40

Earlier quoted context omitted.

What’s confusing? I must’ve missed it in my cursory scan.

Dog and Person are structurally the same so you can assign a person to a dog and vice versa. But that's just how structural typing works and as a user of TypeScript I haven't run into a case where this'd be an issue.

That's the most they could simplify the code to make the point?

Re: Moving from TypeScript to Rust / WebAssembly

#50

Whenever I write Rust, I have a lot of fun, but I'm still not sold on it for most web dev. The analogy that comes to mind is that Rust is a really nice sports car with a great engine. It handles like a dream and you can feel the powerful engine humming while driving it. With the right open road, it's a great time. You can really optimize code, make great abstractions and work with data easily. Unfortunately, web dev…

For backend web development I was somewhat disappointed to see that many of the new web frameworks (all async) allocate extensively on the heap (for example lots of Strings in their http request types.)

I mean it works but I don't understand why I would go to the bother of thinking about lifetimes when it seems performance would be similar to Kotlin / Swift / F#.

Post reply on HN