- cross-posted to:
- programmerhumor@lemmy.ml
- cross-posted to:
- programmerhumor@lemmy.ml
Made with KolourPaint and screenshots from Kate (with the GitHub theme).
You’re encoding more information in the typescript one. You’re saying it’s a string that will get updated.
Yeah, it’s explicitly distinct from
const a: String
which says it won’t change, andvar a: String
, which means this is legacy code that needs fixing.If there’s only two options you only need one keyword
That looks like rust ngl
python:
a: str = 1
And then assign an int to a string just to mess with the interpreter.
only the linter gives a hoot - the interpreter will happily leave that footgun for later
I wish the interpreter cared about assignment
Because sometimes that
let
can be replaced by other things likeconst
. Which can be managed statically by the machine and not by my (imperfect) ability to know if it’s mutated or notI think you can do
const thing = ... as const
to lock down the mutation?
Can we talk about PHP functions with typehints too?
public static function foo(): string {
Practically every other language with similar syntax does this instead:
public static string foo() {
Rust and TypeScript use the return-type-at-the-end convention as well.
TypeScript doesn’t need the “function” keyword for a method in an object or on a class though.
const foo = { bar(): string { ... } }
which I assume is doable because the syntax is unambiguous.
PHP’s object orientation is similar to languages like Java and C#, which is what I was comparing to.
Your example didn’t mention the use of the function keyword. Instead, it seemed to be questioning the placement of the return type - placing it after the argument list seems pretty common in newer languages.
Python too.
TIL PHP has statics.
Also, does PHP actually enforce the type declarations? I’d assume it would but knowing PHP…
Not to short-circuit the joke, but in this case, it’s because the valid JavaScript version is…
let a
…and one of TypeScript’s main design goals is to be a superset of JavaScript, that only adds syntax, and doesn’t re-write it.
Beyond that, it’s probably a case of some new language just using what the designer is familiar with.
let a: &'static str
The actual reason why let … in syntax tends to not use C-style “type var” like syntax is because it’s derived from the syntax type theory uses, and type theorists know about parameterised types. Generics, in C++ parlance, excuse my Haskell:
let foo :: Map Int String = mempty
We have an empty map, and it maps integers to Strings. We call it foo. Compare:
Map Int String foo = mempty
If nothing else, that’s just awkward to read and while it may be grammatically unambiguous (a token is a name if it sits directly in front of
=
) parser error messages are going to suck.Map<Int,String>
is also awkward but alas that’s what we’re stuck with in Rust because they reasoned that it would be cruel to put folks coming from C++ on angle bracket withdrawal. Also Rust has ML ancestry don’t get me started on their type syntax.There is also the thing where the compiler might mistake your c++ style variable declaration for a function, e.g.
String myfunction():
String myvariable();
String a: new String()
=
?