TiarkRompf / virtualization-lms-core

A Framework for Runtime Code Generation and Compiled DSLs
http://scala-lms.github.com
BSD 3-Clause "New" or "Revised" License
324 stars 91 forks source link

Rep[Unit] not always inferred #67

Closed asujeeth closed 9 years ago

asujeeth commented 11 years ago

Blocks that return Rep[Unit], e.g. foreach, sometimes fail if the last statement is not actually Unit. The error message is often confusing, and requires users to manually insert a final "()".

julienrf commented 11 years ago

Can this be solved by adding the following implicit conversion?

implicit def any2RepUnit[A](a: Rep[A]): Rep[Unit] = unit(())

Anyway, IMHO, the fact that the Scala compiler allows to return any value for Unit is a bug. Programmers should always explicitly write () if their expression has not type Unit

vjovanov commented 11 years ago

I would not use the suggested implicit. Rep[Unit] <: Rep[AnyVal] which means that you can assign anything to Rep[AnyVal] and that is a soundness issue.

What is the meaning of Rep[Unit]? Maybe it should not exist in the first place and we should just use Unit. Then the regular Scala conversion would work well.

julienrf commented 11 years ago

I would not use the suggested implicit. Rep[Unit] <: Rep[AnyVal] which means that you can assign anything to Rep[AnyVal] and that is a soundness issue.

I agree.

What is the meaning of Rep[Unit]?

A delayed computation of type Unit. I don’t think we can use Unit instead of Rep[Unit]: how would you then differentiate delayed computations from “usual” computations?

vjovanov commented 11 years ago

Yes it is delayed, but there is nothing you can do on it except for AnyVal operations which are anyhow infix. http://www.scala-lang.org/api/current/index.html#scala.Unit

The idea here is to give up staging of unit for the nicer interface.

TiarkRompf commented 9 years ago

Doesn't seem like much can be done about this at the framework level.