by cellis · 5y ago · 0 comments
I think it is a fair point. Staying within a language and an eco-system of libraries, tools etec. makes sense so you can get profiency and just know how things work without too much thinking about it.
However it only holds to an extent as many languages are tied to or are native to a particular platform. I.e. JavaScript in a web browser, Java on the JVM, Java in Android, Objective-C in iOS etc. And some are definately not mature when you go outside their habitat. Try to compile Java into JavaScript (using GWT or whatever), or into a binary. You can do it however it doesn't really makes sense, so much of a language is really its libraries and way doing things. You are not going to get HSQLDB and JPA running inside a web browser for any reasonable application.
TypeScript fails me almost exclusively due to `!`. The association is so strong that whenever I encounter an error, I am excited to see if it's due to `!`. In the absence of non-null type assertions, TS feels very safe to me, largely due to experience of not seeing it fail.
export function assertIsDefined<V>(
value: V,
buildMessage = defaultAssertIsDefinedMessage
): asserts value is NonNullable<V> {
if (value === undefined || value === null) {
throw new Error(buildMessage(value));
}
}
It would prevent follow up errors if the value in fact was not defined.For example, suppose I have a variable of type
type Name = string | null
const name : Name = null
and I pass it to a function that expects just a string (not null), function len(str: string) {
return str.length;
}
Then len(name);
will fail type checking, but len(name!);
will not fail type checking, but will fail at runtime.[0] https://stackoverflow.com/questions/38874928/operator-in-typ...
Do you know why it was added? Ive used an annotation that disables type checking on a couple unit tests, but this seems much harder to notice
That's not true, right? AFAIK, the Java type system is broken and any type might silently be another type which doesn't support any of the methods the static type would indicate. It's basically as type safe as C in my eyes.
> any type might silently be another type which doesn't support any of the methods the static type would indicate
When an object is typed differently from its actual runtime type, it's usually because it's hidden behind an interface type. So it indicates fewer methods than it supports. Safe, but unergonomic!
In Java, every access of an object variable can blow up your program even though the type checker is happy. In C, every access of a pointer variable can blow up your program. This seems very similar. Neither language provides type safety.
EDIT: To be clear, Java is definitely safer than C. When Java "blows up" due to a null variable access, it throws an exception in a well-defined way. When C blows up due to a null pointer dereference, anything can happen, and if you're lucky your program terminates unconditionally. It's just that neither error is caught by the type system.
int square(char* string) {
int *number = (int*)(void*)string;
return *number * *number;
}
It's UB, so in principle it can do anything, and in practice, the best thing it might do is return gibberish.The Java version of this would throw a ClassCastException on the first line.