7

あいまいなタイトルで申し訳ありません...これを特徴付ける方法がわかりませんでした。

しばらくの間、Scala で特定のコード構成を見たり使用したりしましたが、それがどのように機能するかわかりません。次のようになります (スプレー ルーティングの例)。

path( "foo" / Segment / Segment ) { (a,b) => {  // <-- What's this style with a,b?
...
}}

この例では、パス内のセグメントは、関連付けられたブロック内でそれぞれ a と b にバインドされます。このパターンの使用方法は知っていますが、どのように機能しますか? 何かを「foo」にバインドしなかったのはなぜですか?

ここで自分の目的のためにスプレーがどのように機能するかについてはあまり興味がありませんが、これは Scala のどのような機能であり、自分でどのように記述すればよいでしょうか?

4

4 に答える 4

0
  1. Why don't you look into source?
  2. As for me, it could be implemented as follows

    • method path takes arbitrary type parameter, some pattern-object of that type and a function from that type:

      def path[T](pattern:Pattern[T])(function:Function[T, `some other type like unit or any`])
      
    • the pattern is constructed with two tricks.

      • The String is either "pimped" to have method / or has an implicit convertion to Pattern[Nothing]
      • the Pattern[T] has method / that constructs another pattern with some new type. The method takes single argument (some ancestor of Segment). I guess — Pattern[T2]:

        trait Pattern[T] {
            ///
            def `/`[T2](otherPattern:Pattern[T2]):Pattern[(T,T2)] 
        }
        
  3. So the first argument of path allows to determine the constructed type of pattern as being the pair. Thus we get proper type for the second argument.
  4. The actual matching work is done inside path. I thought it was out of the questions scope.
于 2013-08-23T06:17:52.327 に答える