Re:Are dynamic languages going to replace static languages? ...
by eloisius on Wednesday August 04, 2021 @02:18AM
(#28057796)
Rust’s traits come to mind. If it quacks like an Iterable it’s an Interable
[ Parent
| on HN ]
Re:Are dynamic languages going to replace static languages? ...
by setr on Wednesday August 04, 2021 @02:34AM
(#28057908)
Rust traits have the opposite behavior. If it says it’s an iterable, it must quack like an iterable.
Something that quacks like an iterable cannot be used as an iterable, unless the trait is explicitly stated — unlike python, where it simply has to quack.
Re:Are dynamic languages going to replace static languages? ...
by yxhuvud on Wednesday August 04, 2021 @03:37AM
(#28058335)
Crystal would be a better example of a statically compiled language with duck typing.
Re:Are dynamic languages going to replace static languages? ...
by NtrllyIntrstd on Wednesday August 04, 2021 @02:24AM
(#28057841)
Typeclasses/traits are not duck typing. A type statically needs to implement an abstract data type (with its methods and invariants). This is more of a categorization/composition effort.
Re:Are dynamic languages going to replace static languages? ...
by benrbray on Wednesday August 04, 2021 @04:57AM
(#28058786)
afaik Rust traits were directly inspired by the typeclass system in Haskell, which came about as a type-safe way to do the kind of ad-hoc polymorphism that you can do with interfaces in object-oriented languages like Java. Here's [1] a good talk by SPJ:
[1] https://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-bu...
Re:Are dynamic languages going to replace static languages? ...
by zozbot234 on Wednesday August 04, 2021 @02:23AM
(#28057838)
On the contrary, the point of type classes/traits is to provide a type-based alternative to simple overloading.
Re:Are dynamic languages going to replace static languages? ...
by shadowfox on Wednesday August 04, 2021 @02:24AM
(#28057840)
What feature of rust traits make them "dynamic" as opposed to the traditional notion of typed interfaces? The latter had been part of static languages for quite a while.
Re:Are dynamic languages going to replace static languages? ...
by eloisius on Wednesday August 04, 2021 @03:05AM
(#28058111)
I'm probably way in over my head with making a good case for how this resembles dynamic languages, but what I had in mind was trait objects. You can box a struct and refer to it as a trait object (aka type erasure). Thereafter, the receiver doesn't care _what_ concrete implementation it gets, just that it implements a certain trait.
Re:Are dynamic languages going to replace static languages? ...
by 4ad on Wednesday August 04, 2021 @05:06AM
(#28058825)
What you describe are existential types, a feature that every statically-typed language with parametric polymorphism has, either through some explicit syntactic feature (Rust traits, Haskell type classes, ML modules), or even without, as from basic logic and the Curry–Howard correspondence you can always encode an existential using universals.
∃a.P(a)
----------------- Remove-Exist
∀b.(∀a. P(a)→b)→b
Some static languages had existentials even before they got parametric polymorphism (Go interfaces). This is why we could do so much in Go without generics!
Re:Are dynamic languages going to replace static languages? ...
by lmm on Wednesday August 04, 2021 @03:28AM
(#28058266)
Yeah that's traditional subtyping, not a dynamic language thing at all.
Re:Are dynamic languages going to replace static languages? ...
by 4ad on Wednesday August 04, 2021 @05:08AM
(#28058837)
Rust traits are not subtyping, Rust's type system is principal (although with subtyping you could achieve the same thing as described by the OP, e.g. in C++, C#, etc, which only have subtyping but no existentials exposed as a syntactic feature).
Re:Are dynamic languages going to replace static languages? ...
by lmm on Wednesday August 04, 2021 @09:12PM
(#28068546)
The only reason trait objects aren't subtyping is that impl trait is not a first class type in Rust. They behave like subtyping in this context.
Re:Are dynamic languages going to replace static languages? ...
by monocasa on Wednesday August 04, 2021 @03:15AM
(#28058187)
Not really traits per se, but std::any::Any is implemented by most Rust types and is pretty dynamic.
Re:Are dynamic languages going to replace static languages? ...
by NtrllyIntrstd on Wednesday August 04, 2021 @02:41AM
(#28057952)
Ever heard of &dyn traits?
(that's a joke)