Questions tagged [structural-typing]

Structural typing is the ability of a type system to identify a type based on what data it contains, rather than how a type is named.

Most programming languages identify types based on their names. For example, in a nominally typed language the types A and B in the code below are different, because they are named differently.

struct A { int a; float b; }
struct B { int a; float b; }

In a structurally typed programming language (which includes OCaml, Haskell, and Scala), the types A and B are the same, because they both contain an int a and a float b.

The Wikipedia page contains a more detailed introduction, and WikiWikiWeb has discussions about the differences between Nominative and Structural typing.


Generally, anything about structural typing should be acceptable on StackOverflow, as long as it does not fit better on Programmers or CSTheory.

116 questions
182
votes
1 answer

Getting a structural type with an anonymous class's methods from a macro

Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
25
votes
8 answers

Duck typing, must it be dynamic?

Wikipedia used to say* about duck-typing: In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid…
24
votes
2 answers

Funny observation about (recursive) structural types in Scala

I needed some recursive structural type in some piece of code using with traits and the structural type as type parameter constraint. It worked fine, but later I learned Scala does not support recursive structural types. So can someone explain me…
urso
  • 894
  • 1
  • 7
  • 16
21
votes
2 answers

Pattern matching structural types in Scala

Why does this print wtf? Does pattern matching not work on structural types? "hello" match { case s: { def doesNotExist(i: Int, x: List[_]): Double } => println("wtf?") case _ => println("okie dokie") }
Mitch Blevins
  • 12,816
  • 3
  • 41
  • 32
20
votes
1 answer

accept multiple types for a parameter in scala

I have two objects, ObjectA and ObjectB, both with a method update(). I want to write a function that accepts either ObjectA or ObjectB (but no other types). Conceptually, this is what I am trying to do: def doSomething[T <: ObjectA | T <:…
Jason Wheeler
  • 834
  • 9
  • 22
18
votes
4 answers

Does C# have an equivalent to Scala's structural typing?

In Scala, I can define structural types as follows: type Pressable = { def press(): Unit } This means that I can define a function or method which takes as an argument something that is Pressable, like this: def foo(i: Pressable) { // etc. The…
Tom Morris
  • 3,889
  • 2
  • 21
  • 42
18
votes
3 answers

Scala: difference between a typeclass and an ADT?

What are the differences between typeclasses and Abstract Data Types? I realize this is a basic thing for Haskell programmers, but I come from a Scala background, and would be interested in examples in Scala. The best I can find right now is that…
James McCabe
  • 1,819
  • 2
  • 13
  • 22
18
votes
2 answers

What is "Structural Typing for Interfaces" in TypeScript

In his blog post about TypeScript, Mark Rendle is saying, that one of the things he likes about it is: "Structural typing for interfaces. I really wish C# could do that" What did he mean by that?
Eran Medan
  • 41,875
  • 56
  • 175
  • 268
17
votes
2 answers

Why scala uses reflection to call method on structural type?

If function accepts structural type, it can be defined as: def doTheThings(duck: { def walk; def quack }) { duck.quack } or type DuckType = { def walk; def quack } def doTheThings(duck: DuckType) { duck.quack } Then, you can use that function in…
Op De Cirkel
  • 26,245
  • 6
  • 37
  • 52
13
votes
1 answer

Scala: Accessing package visible methods through structural types outside the package

This does not work as expected (since I am trying to call a package private run from outside Services): object Services { class HelloPrinter { private[Services] def run = "Hello" } } val obj = new Services.HelloPrinter But, surprisingly…
pathikrit
  • 29,060
  • 33
  • 127
  • 206
10
votes
4 answers

Clojure Protocols vs Scala Structural Types

After watching the interview with Rich Hickey on Protocols in Clojure 1.2, and knowing very little about Clojure, I have some questions on Clojure Protocols: Are they intended to do the same thing as Structural Types in Scala? What benefits do…
Vasil Remeniuk
  • 20,009
  • 5
  • 68
  • 81
10
votes
1 answer

Using Scala structural types with abstract types

I'm trying to define a structural type defining any collection that has an "add" method (for instance, a java collection). Using this, I want to define a few higher order functions that operate on a certain collection object GenericTypes { type…
Joshua Hartman
  • 1,196
  • 1
  • 10
  • 18
10
votes
4 answers

Why does Haskell not have records with structural typing?

I have heard Haskell described as having structural typing. Records are an exception to that though as I understand. For example foo cannot be called with something of type HRec2 even though HRec and HRec2 are only nominally different in their…
user782220
  • 9,199
  • 16
  • 64
  • 127
10
votes
1 answer

Structural typing implementation of OCaml, Scala, and Go

While researching about structural typing I found the following post describing how interfaces in Go are translated to method lookup tables at runtime. The process described in the post seems vastly different than the reflective and generative…
Syllepsis
  • 451
  • 3
  • 12
9
votes
6 answers

Why interfaces must be declared in Java?

Sometimes we have several classes that have some methods with the same signature, but that don't correspond to a declared Java interface. For example, both JTextField and JButton (among several others in javax.swing.*) have a method public void…
leonbloy
  • 65,169
  • 19
  • 130
  • 176
1
2 3 4 5 6 7 8