Quantcast

Type incantation?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Type incantation?

David Pennell
I added a function to my parser to help with a common construct:

  def paren(rule) = LeftParen ~ rule ~ RightParen

I need help with adding a type spec to this so it will accept all the various forms of Rules.  I'm new to Scala, have been reading and re-reading various sources, but haven't found the right combination yet.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Type incantation?

mathias
Administrator
David,

unfortunately it's not possible to write such a function generically for _all_ rules, since the the result type of your `paren` rule depends on the type of its argument in a way that cannot fully be abstracted over.

E.g. if your `rule` argument is of type `Rule0`, i.e. does not produce any value on the value stack then the thing looks like this (assuming your `LeftParen` and `RightParen` rules are `Rule0` rules):

        def paren(rule: Rule0): Rule0 = LeftParen ~ rule ~ RightParen

For all `Rule1` rules on the contrary this is the way to write it:

        def paren[T](rule: Rule1[T]): Rule1[T] = LeftParen ~ rule ~ RightParen

For `Rule2` rules:

        def paren[A, B](rule: Rule2[A, B]): Rule2[A, B] = LeftParen ~ rule ~ RightParen

You see where this is going...

HTH and cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 21.01.2012, at 08:24, David Pennell [via parboiled users] wrote:

> I added a function to my parser to help with a common construct:
>
>   def paren(rule) = LeftParen ~ rule ~ RightParen
>
> I need help with adding a type spec to this so it will accept all the various forms of Rules.  I'm new to Scala, have been reading and re-reading various sources, but haven't found the right combination yet.
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Type-incantation-tp3677429p3677429.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Type incantation?

David Pennell
Thanks!  That is just what I needed.
Loading...