का उपयोग कर निम्नलिखित सार एक विचार मैंमुद्दे के समाधान arity सूची प्रसंस्करण ड्राइव करने के लिए, निराकार
package com.test1
import scala.language.implicitConversions
import shapeless._
import FromTraversable._
import Traversables._
import Nat._
import Tuples._
trait ToArity[P, N <: Nat]
object ToArity {
implicit def prod1[P <: Product1[_]] = new ToArity[P, _1] {}
implicit def prod2[P <: Product2[_, _]] = new ToArity[P, _2] {}
// ad nauseum...
}
trait SizedHListAux[A, N <: Nat, T <: HList]
object SizedHListAux {
implicit def base[A, H <: HList] = new SizedHListAux[A, _0, HNil] {}
implicit def induct[A, H <: HList, N <: Nat, P <: Nat](implicit r: PredAux[N,P], k: SizedHListAux[A, P, H]) = new SizedHListAux[A, N, A :: H] {}
}
trait SomeFun {
type Result
def apply(): Result
}
// I want to abstract over A, the contained type in the List
// over P the Product type which is the arg notably its arity
// This means we need to recover arity of the Product type and render it in value space
// and also means that we need to compute the type of the intermediate HList
object SomeFun {
def produce(m: SomeFun): m.Result = m()
implicit def fromF1[T, A, P <: Product, N <: Nat, H <: HList](f1: (P => T, List[A]))(implicit k: ToArity[P, N], toI: ToInt[N], l: SizedHListAux[A, N, H], toHL: FromTraversable[H], tp: TuplerAux[H, P]) =
new SomeFun {
type Result = (T, List[A])
def apply(): Result = {
val (f, as) = f1
val (ts, rest) = (as.take(toI()), as.drop(toI()))
f((toHL(ts).get).tupled) -> rest
}
}
// Debug Arity checker
def printArity[P <: Product, N <: Nat](p: P)(implicit k: ToArity[P, N], toI: ToInt[N]) = println("Arity: " + toI())
}
object Test {
val thedata = List("foo", "bar", "baz", "bob")
val tfn = (x: (String, String)) => println("%s and %s".format(x._1, x._2))
def foo = SomeFun.printArity("a" -> "b")
//def doit = SomeFun.produce((tfn, thedata)) // Adding this line does not compile
}
विचार के साथ खेल रहा हूँ के लिए कोड है कि आप इस मामले में एक समारोह के तर्क arity उपयोग करते हैं, है किसी संबंधित सूची की पार्सिंग को चलाने के लिए उत्पाद प्रकार की धर्मार्थता [ए]। ग्रेफाइट से graphene की परतों को छीलने के लिए चिपचिपा टेप का उपयोग करने की तरह, यानी कार्यों के प्रकार सूची से बाहर खींचें। यह केवल एक निहित प्रकार का उपयोग करके एक स्केच है, लेकिन मुझे लगता है कि इसे सामान्यीकृत किया जा सकता है। महत्वपूर्ण पहलू यह है कि कार्य स्वयं सूची प्रसंस्करण से अनजान हैं।
हालांकि ... अवधारणा को हल करने की कोशिश करते समय अवधारणा विफल होने लगती है [पी, एन] निहित। प्रिंटऑरिटी() द्वारा प्रमाणित के रूप में अपने स्वयं के ToArity पर हल करने योग्य है।
क्या कोई इस बात पर कुछ प्रकाश डाल सकता है कि यह एफ 1 के संदर्भ में क्यों हल नहीं किया जा सकता है? क्या यह सभी निर्भर निहितार्थों को हल नहीं कर सकता है और फिर पहली बार त्रुटि को पंजीकृत कर सकता है, यानी एन एन को ToArity, ToInt और SizedHListAux को संतुष्ट करने के लिए नहीं मिला है?
एएच! अब समझ में आता है, मानक बेकार घटकों का उपयोग करने पर पॉइंटर्स के लिए धन्यवाद ... उम्मीद कर रहा था कि मैं कुछ फन के माध्यम से अलग-अलग उपज() रूपों पर सार के लिए अप्रत्यक्ष हो सकता हूं लेकिन अब यह एक कदम की तरह लगता है। – dconlon