from Draft addition to “The Difference Between Named Type Systems and Structural Type Systems” Difference between a nomenclature-type system and a structural-type system.

Dart: Currently, typedefs are restricted to function types. We expect this to change.

I was told at a TypeScript study group that “we study types in Technology Supporting Coding” and when I checked, there was still no explanation of the difference between structual and nominal. I would like to add it.

brainstorming Type hint flow for dynamically typed languages Later research on language rankings for 2014

TypeScript was released in 2012 2019

The dictionary eliminates the need to clarify in advance what attributes are available. Hackers and painters

  • Pencil for drawing But the undefined trap
  • "" + 1
    • Static Typing Language “Error
    • Python runtime error TypeError: cannot concatenate ‘str’ and ‘int’ objects
    • JavaScript “1”
  • {}["x"]
    • Python: KeyError
    • JavaScript: undefined
    • This is not the difference between static typing and dynamic typing, just that JavaScript is particularly bad among dynamically typed (dynamic checking) languages. The point of discovery is far away from the problem area. Implementation in js rapidly expanded by the needs of society Execution occurs on the client side Strong need to be aware of problems in advance
  • Two approaches

Python Type hints

Liskov’s replacement principle

  • Subtypes may be used in place of supertypes.”

  • Subtyping is not subclassing, thesis

    • Cook, W.R.; Hill, W.L.; Canning, P.S. (January 1990). “Inheritance is not subtyping”. Proceedings of the Seventeenth Annual ACM Symposium on Principles of Programming Languages. San Francisco, California: 125–135.
  • Java was released in 1995, so this paper was out

    • In Introduction to Type Systems, “19.3 Named Type Systems and Structural Type Systems”.
    • Structual was more mainstream as a type study, the system is simple.
    • nominal is a type always accompanied by a name
    • class Cat extends Animal { … }
      • They are making a “named mold” called Cat.
      • Easy to verify if it is a subtype
    • Maybe Nominal was adopted in Java, etc. because of performance issues.
      • The rumor that “Scala is slow.”
      • Later research has created a method for fast identification even with Structual.
        • Create a hash of type
      • Did these areas directly contribute to the increase or not?
      • It’s been 15 years since Scala was born in 2004, so the machines are probably more powerful now.
        • (Would the SSD conversion be greater than the CPU performance?)
        • Did improved compile times contribute to the popularity of the Structural Typing language?
  • Talking about Go’s interface, there was no place to write it.

  • https://stackshare.io/stackups/dart-vs-typescript

    • TypeScript appears to be popular.
    • In TIOBE, though, it was Dart 0.5%, TypeScript 0.2%.
    • I guess it’s a situation where there are too few of either and the small and large are reversed in the slightest situation.
  • the more important idea is the separation of concept: data and behavior are two distinct concepts in Go, not conflated into a single notion of “class”.

  • https://en.wikipedia.org/wiki/Structural_type_systemに

  • C++ template functions exhibit structural typing on type arguments.

  • but what exactly does this mean in terms of behavior?

    • (moriyoshit)Operations on members of a type given in a type parameter will pass the compile if the structure matches whatever the type is, so either to that effect and

    • This is what you mean? cpp
struct A{
  int i;
  int j;
};
 
struct B{
  int i;
  int k;
};
 
template<class T>
int get_i(T& x){
  return x.i;
};
 
int main(){
  A a;
  B b;
  get_i<A>(a);
  get_i<B>(b);
}
    - Is this Structural Typing? It's just that two implementations of get_i have been created that are specialized with the given type parameters, and each one is compiled normally.
        - In a normal compilation, the code "x.i" succeeds whether x is A or B. Why not call it "Structural"?
    - Only the `get_i<T>` part is of interest: whether a type can enter T is "determined by the structure, not the name" of "the type that has member i."

cpp

struct A{
  int i;
  int j;
};
 
struct B{
  int i;
  int k;
};
 
struct C{
  int j;
  int k;
};
 
template<class T>
int get_i(){
  T x;
  return x.i;
};
 
int main(){
  get_i<A>();
  get_i<B>();
  //get_i<C>();  // error: no member named 'i' in 'C'
}
  • Generics (generic type) was introduced in Java in 5.0, 2004.

    • Nominal languages have such “generic” features… Some of them have extensions of the “generic” function. But those with such extensions are no longer purely nominal type systems, but rather a somewhat complex hybrid of the two approaches. Designers of languages with evolving type functions tend to opt for the structural approach. - Introduction to Type Systems P.198

    • When were templates introduced to C++?
    • I think that the nominal mechanism is no longer purely nominal, and that the Stractural mechanism is gradually being mixed in with the Nominal mechanism around that time.
  • Constraints and concepts (since C++20) - cppreference.com

  • (Java adopted the generic (generic type) feature in 2004. (Java adopted generic types in 2004. This is the direction in which Java, which used to be a nametype type, is now moving toward a hybrid with structural types, but whether or not to devote a page to explaining this is a matter for discussion).

    • In short, in Java, if a type had a different name or was not in an inheritance relationship, it was a different type altogether, especially for containers, where different types T1 and T2 shared the “can be placed in that container” condition.
      • If it had certain characteristics, it was good to containerize it.
      • I guess you could say that once I started to understand this area, I realized that there are a lot of problems with pure Nominal.
  • STL in 1993.

  • function templates, class templates and, since C++14, variable templates. Since C++11


This page is auto-translated from /nishio/「名前的型システムと構造的型システムの違い」加筆案メモ using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I’m very happy to spread my thought to non-Japanese readers.