{"id":4026,"date":"2014-03-30T07:16:29","date_gmt":"2014-03-30T07:16:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-do-i-create-an-instance-of-a-trait-in-a-generic-method-in-scala-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:16:29","modified_gmt":"2014-03-30T07:16:29","slug":"how-do-i-create-an-instance-of-a-trait-in-a-generic-method-in-scala-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-do-i-create-an-instance-of-a-trait-in-a-generic-method-in-scala-collection-of-common-programming-errors\/","title":{"rendered":"How do I create an instance of a trait in a generic method in scala?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m not sure what the motivation is for your question, but you could consider passing a factory for <code>T<\/code> as an implicit parameter. This is known as using <em>type classes<\/em> or <em>ad-hoc polymorphism<\/em>.<\/p>\n<pre><code>object Test extends Application {\n  trait Factory[T] {\n    def apply: T\n  }\n  object Factory {\n    \/**\n     * Construct a factory for type `T` that creates a new instance by\n     * invoking the by-name parameter `t`\n     *\/\n    def apply[T](t: =&gt; T): Factory[T] = new Factory[T] {\n      def apply = t\n    }\n  }\n\n  \/\/ define a few traits...\n  trait T1\n  trait T2\n\n  \/\/ ...and corresponding instances of the `Factory` type class.\n  implicit val T1Factory: Factory[T1] = Factory(new T1{})\n  implicit val T2Factory: Factory[T2] = Factory(new T2{})\n\n  \/\/ Use a context bound to restrict type parameter T\n  \/\/ by requiring an implicit parameter of type `Factory[T]`\n  def create[T: Factory]: T = implicitly[Factory[T]].apply\n\n  create[T1]\n  create[T2]\n\n}\n<\/code><\/pre>\n<p>At the other end of the spectrum, you could invoke the compiler at runtime, as detailed in this answer to the question &#8220;Dynamic mixin in Scala &#8211; is it possible?&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m not sure what the motivation is for your question, but you could consider passing a factory for T as an implicit parameter. This is known as using type classes or ad-hoc polymorphism. object Test extends Application { trait Factory[T] { def apply: T } object Factory { \/** * Construct a factory for type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4026","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4026"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4026\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}