- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Could quasiquotes do this?
Sun, 2012-02-05, 20:49
One thing that I'm finding myself wanting quite frequently these days
is a way of having the compiler verify for me that something otherwise
well-formed doesn't in fact typecheck. The test suite and examples for
shapeless are littered with commented out lines like,
def acceptOption[L <: HList : TC[Option]#λ](l : L) = true
val l1 = Option(23) :: Option(true) :: Option("foo") :: HNil
val l2 = Option(23) :: true :: Option("foo") :: HNil
acceptOption(l1) // Compiles
//acceptOption(l2) // Does not compile
which is pretty useless if I should ever break things in such a way
that the last line actually _did_ compile.
Is there something that quasiquotation could do for us here? Would it
be possible to support something like,
typeError(acceptOption(l2))
which typechecks iff acceptOption(l2) is a type error (but not if, eg.
l2 is undefined)?
Cheers,
Miles
Sun, 2012-02-05, 21:11
#2
Re: Could quasiquotes do this?
On Sun, Feb 5, 2012 at 7:59 PM, Eugene Burmako wrote:
> This maps directly onto macros.
>
> You can write typeError as a macro, which accepts an untyped argument
> (currently not implemented, but soon will be:
> https://issues.scala-lang.org/browse/SI-5404). Then you just take this
> argument (an AST) and typecheck it using services of
> scala.reflect.macro.Context (typechecking is not yet there, but it will be
> eventually). If typechecking returns an error, you just return from a macro.
> If not, you raise an error yourself (using some API that is still not in
> place).
>
> All in all, this can be done with macros, though it has to wait for the next
> macro-related pull request.
Woo hoo! Good stuff :-)
Cheers,
Miles
Sun, 2012-02-05, 21:11
#3
Re: Could quasiquotes do this?
On Sun, Feb 5, 2012 at 8:03 PM, Miles Sabin wrote:
> On Sun, Feb 5, 2012 at 7:59 PM, Eugene Burmako wrote:
>> This maps directly onto macros.
>>
>> You can write typeError as a macro, which accepts an untyped argument
>> (currently not implemented, but soon will be:
>> https://issues.scala-lang.org/browse/SI-5404). Then you just take this
>> argument (an AST) and typecheck it using services of
>> scala.reflect.macro.Context (typechecking is not yet there, but it will be
>> eventually). If typechecking returns an error, you just return from a macro.
>> If not, you raise an error yourself (using some API that is still not in
>> place).
>>
>> All in all, this can be done with macros, though it has to wait for the next
>> macro-related pull request.
>
> Woo hoo! Good stuff :-)
Hmm ... although, now I think about it, are you sure this wouldn't
allow me to construct a type-level liar?
Cheers,
Miles
Sun, 2012-02-05, 21:21
#4
Re: Could quasiquotes do this?
On Feb 5, 2012, at 12:48 PM, Miles Sabin wrote:
> Is there something that quasiquotation could do for us here? Would it
> be possible to support something like,
>
> typeError(acceptOption(l2))
>
> which typechecks iff acceptOption(l2) is a type error (but not if, eg.
> l2 is undefined)?
This would be really great to have. It could be even easier if the tree a macro def gets doesn't need to be well typed. The macro could then rewrite the tree to be well typed. In this case it would rewrite an ill typed tree to a unit value and a well typed tree to an error value.
Sun, 2012-02-05, 21:31
#5
Re: Could quasiquotes do this?
Type-level liar?
On Feb 5, 9:08 pm, Miles Sabin wrote:
> On Sun, Feb 5, 2012 at 8:03 PM, Miles Sabin wrote:
> > On Sun, Feb 5, 2012 at 7:59 PM, Eugene Burmako wrote:
> >> This maps directly onto macros.
>
> >> You can write typeError as a macro, which accepts an untyped argument
> >> (currently not implemented, but soon will be:
> >>https://issues.scala-lang.org/browse/SI-5404). Then you just take this
> >> argument (an AST) and typecheck it using services of
> >> scala.reflect.macro.Context (typechecking is not yet there, but it will be
> >> eventually). If typechecking returns an error, you just return from a macro.
> >> If not, you raise an error yourself (using some API that is still not in
> >> place).
>
> >> All in all, this can be done with macros, though it has to wait for the next
> >> macro-related pull request.
>
> > Woo hoo! Good stuff :-)
>
> Hmm ... although, now I think about it, are you sure this wouldn't
> allow me to construct a type-level liar?
>
> Cheers,
>
> Miles
>
> --
> Miles Sabin
> tel: +44 7813 944 528
> gtalk: mi...@milessabin.com
> skype: milessabin
> g+:http://www.milessabin.comhttp://twitter.com/milessabinhttp://www.chuusai.com/
Sun, 2012-02-19, 07:11
#6
Re: Could quasiquotes do this?
As a pre-macros approach to this:
Rogue includes some tests which check that certain expressions don't type-check. It does this by embedding an interpreter in the test suite and checking the output of the interpreter.
See the "thingsThatShouldntCompile" test: https://github.com/foursquare/rogue/blob/master/src/test/scala/com/foursquare/rogue/QueryTest.scala#L562
--j
On Sun, Feb 5, 2012 at 2:48 PM, Miles Sabin <miles@milessabin.com> wrote:
Rogue includes some tests which check that certain expressions don't type-check. It does this by embedding an interpreter in the test suite and checking the output of the interpreter.
See the "thingsThatShouldntCompile" test: https://github.com/foursquare/rogue/blob/master/src/test/scala/com/foursquare/rogue/QueryTest.scala#L562
--j
On Sun, Feb 5, 2012 at 2:48 PM, Miles Sabin <miles@milessabin.com> wrote:
One thing that I'm finding myself wanting quite frequently these days
is a way of having the compiler verify for me that something otherwise
well-formed doesn't in fact typecheck. The test suite and examples for
shapeless are littered with commented out lines like,
def acceptOption[L <: HList : TC[Option]#λ](l : L) = true
val l1 = Option(23) :: Option(true) :: Option("foo") :: HNil
val l2 = Option(23) :: true :: Option("foo") :: HNil
acceptOption(l1) // Compiles
//acceptOption(l2) // Does not compile
which is pretty useless if I should ever break things in such a way
that the last line actually _did_ compile.
Is there something that quasiquotation could do for us here? Would it
be possible to support something like,
typeError(acceptOption(l2))
which typechecks iff acceptOption(l2) is a type error (but not if, eg.
l2 is undefined)?
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://www.chuusai.com/
You can write typeError as a macro, which accepts an untyped argument (currently not implemented, but soon will be: https://issues.scala-lang.org/browse/SI-5404). Then you just take this argument (an AST) and typecheck it using services of scala.reflect.macro.Context (typechecking is not yet there, but it will be eventually). If typechecking returns an error, you just return from a macro. If not, you raise an error yourself (using some API that is still not in place).
All in all, this can be done with macros, though it has to wait for the next macro-related pull request.
On 5 February 2012 20:48, Miles Sabin <miles@milessabin.com> wrote: