+ 6

Is PHP the only language that natively supports gradual typing?

Excluding languages derived from existing dynamically typed languages e.g TypeScript.

6th Aug 2020, 4:02 PM
Ore
Ore - avatar
13 odpowiedzi
+ 5
Ore In my humble opinion all these dynamically typed languages from the 90s and 2000s were a misguided attempt to make programming "easier". But I think the community has realized that it leads to messy code and that types are actually helpful when programming. It's probably alright for scripting languages where projects don't get very big. R comes to mind. Maybe Javascript was intended to be one of those but it obviously didn't work out that way :D Typecheckers have gotten better too. If you read a java-esque `FooFactoryFactory bar = new FooFactoryFactory();` then dynamic types might seem like a good idea but these days you can throw an `auto` at a C++ complier and it just figures it out. No point in not having types. I don't think many more dynamically typed languages will be made honestly. (And as for C# - it's not that optional, there's a heavy bias towards not using `dynamic` I think. It's rather frowned upon.)
6th Aug 2020, 11:34 PM
Schindlabua
Schindlabua - avatar
+ 4
Ore If gradual typing was another term for type hinting, then Python does support it, though am not sure how similar/different the implementation be. An old intro found from net search, for those who wonders what gradual typing is ... https://wphomes.soic.indiana.edu/jsiek/what-is-gradual-typing/
6th Aug 2020, 4:36 PM
Ipang
+ 4
Ore I didn't even know PHP8 was coming! One thing I regret about PHP is inconsistence in how functions are defined and cryptic error message. Really brought me down because I keep forgetting parameter positions and how return values should be handled 😁 I hope they make big (and good) changes with the new version. Thanks for the news bro! 🙏
6th Aug 2020, 7:01 PM
Ipang
+ 4
I have not heard the term "gradual typing" before but Ruby 3 is adding support for types. I have not looked into the details but it may be a gradual typing thing where some values now have static types. As for C#: we can parse JSON files for example into a `dynamic` object which allows us to use dot-notation on the parsed JSON object, even if we don't know what it looks like before we parse it. That makes a whole lot of sense in Web Applications! Then agian I don't think a C#-style `dynamic` has that many uses and it gives us nothing we can't do without it but I guess a language with already full-blown Reflection support has an easier time implementing that feature. It's kinda handy in the context of C#.
6th Aug 2020, 10:58 PM
Schindlabua
Schindlabua - avatar
+ 3
Ipang Type hinting is quite different from gradual typing. According to Wikipedia ``` Gradual typing is a type system in which some variables and expressions may be given types and the correctness of the typing is checked at compile-time (which is static typing) and some expressions may be left untyped and eventual type errors are reported at run-time (which is dynamic typing) ``` In short it means that you can choose to specify type of a variable or not to. So the type is checked at both compile time or runtime accordingly. https://en.m.wikipedia.org/wiki/Gradual_typing After carefully reading this Wikipedia article. I think I found my answer. Dart and latest C# version are also gradually typed. But that makes me wonder why C#, a statically typed language is improved to become gradually typed. Does that mean there is some advantage of gradual typing over static typing?🤔
6th Aug 2020, 6:21 PM
Ore
Ore - avatar
+ 3
Ore The article I read said something about that in its earlier section. But I couldn't make a sense out of the explanation honestly. It also said something similar to Wki's in regard to definition, optional type definition in some parts of the code. I just can't get my head around the idea of mixing dynamically & statically typed languages in a project without having to worry about language interoperability, all just by adding a relaxation of type system.
6th Aug 2020, 6:39 PM
Ipang
+ 3
Yes pretty much what I understood from the reads also. It was the implementation of interfacing the different type system of the languages that I think be kind of complex.
6th Aug 2020, 6:51 PM
Ipang
+ 3
Schindlabua Thanks. I was wondering why a statically typed language like C# will suddenly make types optional (correct me if I am wrong). It makes sense now. Also, it seems most of the dynamically typed languages, (ruby, php) are suddenly adding support for static types.🤔
6th Aug 2020, 11:09 PM
Ore
Ore - avatar
+ 3
Schindlabua Yeah most of the languages created after 2010 are static typed and are less forgiving than older languages. It seems the community have understood the shortcomings of dynamic typing. But as it seems, languages for the web are all moving towards gradual typing. If this ends up being a bad idea. Then we might end up in a similar situation like we have now. One good advantage of gradual typing in languages that support it is that you can turn it off. It is kind of a win-win. I am not sure if that is possible in C# but in PHP <?php //To switch it on declare(strict_types=1); I guess more web languages will adopt this type system later on.
7th Aug 2020, 6:14 AM
Ore
Ore - avatar
+ 1
Ipang It is not that complex honestly. In a dynamically typed language, variable types are not specified and are derived during run-time. e.g a = func() b = a + 8 There is no way of knowing the type of a until func is evaluated at runtime. Therefore there is no way for the compiler to catch type errors. In statically typed languages, type is defined explicitly in the code and the compiler can determine type while compiling the code. Gradual typing means type may or may not be defined in code. If it is, the compiler determines the type during compile time, if not it is determined during run time and type errors cannot be caught Gradual typing(PHP), class Foo { public int $typed; public $untyped; public ?array $typedButNullable; }
6th Aug 2020, 6:46 PM
Ore
Ore - avatar
+ 1
Ipang Yeah. That is beyond my knowledge scope too 🤓. The PHP interpreter is one of the most confusing among languages I have used. Things will likely get more complex with PHP 8. It is rumoured that PHP 8 will have a JIT compiler.🤯
6th Aug 2020, 6:56 PM
Ore
Ore - avatar
+ 1
Ipang I have heard that complaint many times. I also hate the inconsistent naming of the functions and inconsistent placing of parameters. In fact almost every built-in function is triadic which makes matter worst. However, there is nothing that can be done about that except the entire language is rewritten. There is no chance of that occurring in PHP 8 or even ever as that will break billions of systems! In PHP 8, they are planning to improve type safety even more and launching a JIT compiler for PHP. The JIT compiler may not make significant improvement in the speed of PHP on the web but it may encourage the use of PHP outside of the web. I am looking forward to it.
6th Aug 2020, 7:50 PM
Ore
Ore - avatar
+ 1
I will probably spend the next few days reviewing the type system of Dart and how the developer experience compares to dynamically typed JavaScript. Hopefully it will give me more insight to why gradual typing is relevant.
6th Aug 2020, 8:29 PM
Ore
Ore - avatar