Clojure Protocols vs Scala Structural Types

心已入冬 提交于 2019-12-02 18:33:27

Totally unrelated.

Scala is a statically typed language. Clojure is a dynamically typed language. This difference shapes both of them fundamentally.

Structural types are static types, period. They're just a way to have the compiler prove statically that an object will have a particular structure (I say prove here, but casting can cause bogus proofs as always).

Protocols in Clojure are a way to create dynamic dispatch that is much faster than reflection or looking things up in a map. In a semantic sense they don't really extend the capabilities of Clojure, but operationally they are significantly faster than the mechanisms used before.

Scala traits are a bit closer to protocols, as are Java interfaces, but again there's a static vs dynamic issue. Scala traits must be associated with a class at compile time, similar to Java interfaces. Clojure protocols can be added to a datatype at runtime after the fact even by a third party.

Something like Clojure protocols is possible in Java and Scala through mechanisms like wrapper/proxy patterns or dynamic proxies ( http://download.oracle.com/javase/1.4.2/docs/guide/reflection/proxy.html ). But those will be a heck of a lot clumsier than Clojure protocols and getting object identity right is tricky as well.

The purpose of Protocols in Clojure is to solve the Expression Problem in an efficient manner.

[See: Simple explanation of clojure protocols.]

Scala's solution to the Expression Problem are Implicits. So, semantically, that is the closest equivalent to Clojure Protocols in Scala. (In Haskell, it would be Typeclasses or maybe Type Families.)

As I understood from this introductory blogpost, Closure Protocols are closer to Scala Traits, rather than Structural Types (and thus, cannot be used as a replacement for them, answering my second question):

/* ----------------------- */
/* --- Protocol definition */
/* ----------------------- */

(defprotocol Fly
  "A simple protocol for flying"
  (fly [this] "Method to fly"))

/* --- In Scala */    
trait Fly{
    def fly: String
}

/* --------------------------- */
/* --- Protocol implementation */
/* --------------------------- */

(defrecord Bird [nom species]
  Fly
  (fly [this] (str (:nom this) " flies..."))

/* --- In Scala */    
case class Bird(nom: String, species: String) extends Fly{
    def fly = "%s flies..." format(nom)
}

/* --------------------- */
/* --- Dynamic extension */
/* --------------------- */

(defprotocol Walk
  "A simple protocol to make birds walk"
  (walk [this] "Birds want to walk too!"))

(extend-type Bird
  Walk
  (walk [this] (str (:nom this) " walks too..."))

/* --- In Scala */    
trait Walk{
    def walk = "Birds want to walk too!"
}

implicit def WalkingBird(bird: Bird) = new Walk{
    override def walk = "%s walks too..." format(bird.nom)
}

/* --------------- */
/* --- Reification */
/* --------------- */

(def pig (reify
                Fly (fly [_] "Swine flu...")
                Walk (walk [_] "Pig-man walking...")))

/* --- In Scala */    
object pig extends Fly with Walk{
    def fly = "Swine flu..."
    override def walk = "Pig-man walking..."
}

Other answers speak to the other parts of your question better, but:

Are they implemented through reflections?

No - protocols are compiled to JVM interfaces. Things which implement protocols (reify, defrecord, etc) are compiled to JVM classes which implement the protocol interface, so calls to protocol functions are the same as standard JVM method calls, under the hood.

That was actually one of the motivators for protocols - a lot of Clojure's internal data structures were written in Java, for speed reasons, because there was no way to do full-speed polymorphic dispatch in pure Clojure. Protocols provide that. Clojure still has a lot of Java in its source code, but that could all can now be rewritten in Clojure with no loss of performance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!