structural-typing

Scala: difference between a typeclass and an ADT?

折月煮酒 提交于 2019-12-02 14:32:53
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 typeclasses are "open" and ADT's are "closed". It would also be helpful to compare and contrast typeclasses with structural types. ADTs (which in this context are not Abstract Data Types, which is even another concept, but Algebraic Data Types) and type classes are completely different concepts which solve different problems. ADT, as follows from the

Why interfaces must be declared in Java?

时光怂恿深爱的人放手 提交于 2019-11-30 15:11:16
问题 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 addActionListener(ActionListener l) Now, suppose I wish to do something with objects that have that method; then, I'd like to have an interface (or perhaps to define it myself), e.g. public interface CanAddActionListener { public void

Generalized structural type conformance in Scala

╄→гoц情女王★ 提交于 2019-11-30 15:07:42
I'm interested in the problem of conforming a specific type to a more general structural type. Consider the following examples: trait Sup trait Sub extends Sup type General = { def contra(o: Sub): Unit def co(): Sup def defaults(age: Int): Unit def defaults2(age: Int): Unit def defaults3(first: String): Unit } trait Specific { def contra(o: Sup): Unit // doesn't conform def co(): Sub // conforms def defaults(age: Int, name: String = ""): Unit // doesn't conform def defaults2(name: String = "", age: Int = 0): Unit // doesn't conform def defaults3(first: String = "", last: String = ""): Unit //

Why interfaces must be declared in Java?

亡梦爱人 提交于 2019-11-30 13:49:16
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 addActionListener(ActionListener l) Now, suppose I wish to do something with objects that have that method; then, I'd like to have an interface (or perhaps to define it myself), e.g. public interface CanAddActionListener { public void addActionListener(ActionListener l); } so that I could write: public void myMethod(CanAddActionListener aaa,

Generalized structural type conformance in Scala

泄露秘密 提交于 2019-11-29 22:13:20
问题 I'm interested in the problem of conforming a specific type to a more general structural type. Consider the following examples: trait Sup trait Sub extends Sup type General = { def contra(o: Sub): Unit def co(): Sup def defaults(age: Int): Unit def defaults2(age: Int): Unit def defaults3(first: String): Unit } trait Specific { def contra(o: Sup): Unit // doesn't conform def co(): Sub // conforms def defaults(age: Int, name: String = ""): Unit // doesn't conform def defaults2(name: String = ""

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

痞子三分冷 提交于 2019-11-29 13:24:17
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 object which I pass to this function must have defined for it a method called press() that matches the type signature defined in the type - takes no arguments, returns Unit (Scala's version of void). I can even use the structural type inline: def foo(i: { def press(): Unit }) { // etc. It basically allows the programmer to have all the benefits of duck

Duck typing, must it be dynamic?

我的未来我决定 提交于 2019-11-28 17:54:18
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 semantics, rather than its inheritance from a particular class or implementation of a specific interface. (* Ed. note: Since this question was posted, the Wikipedia article has been edited to remove the word "dynamic".) It says about structural typing : A structural type system (or property-based type system) is a major class of type system, in which type

Pattern matching structural types in Scala

拥有回忆 提交于 2019-11-28 07:27:24
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") } Running this example in the Scala interpreter with unchecked warnings on ( scala -unchecked ) produces the following warning: warning: refinement AnyRef{def doesNotExist(Int,List[_]): Double} in type pattern is unchecked since it is eliminated by erasure . Unfortunately, a generic type like this cannot be checked at runtime as the JVM doesn't have reified generics. All that the JVM sees in this

Scala - how to define a structural type that refers to itself?

孤街浪徒 提交于 2019-11-28 07:03:15
I'm trying to write a generic interpolate method that works on any type that has two methods, a * and a + , like this: trait Container { type V = { def *(t: Double): V def +(v: V): V } def interpolate(t: Double, a: V, b: V): V = a * (1.0 - t) + b * t } This doesn't work though (on Scala 2.8.0.RC7), I get the following error messages: <console>:8: error: recursive method + needs result type def +(v: V): V ^ <console>:7: error: recursive method * needs result type def *(t: Double): V ^ How do I specify the structural type correctly? (Or is there a better way to do this?) Surely you could solve

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

梦想与她 提交于 2019-11-28 07:01:58
问题 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 object which I pass to this function must have defined for it a method called press() that matches the type signature defined in the type - takes no arguments, returns Unit (Scala's version of void). I can even use the structural type inline: def foo(i: