Re: enable_if/disable_if

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: enable_if/disable_if

杨 Yang博 Bo
Scala is able to execute at compiler time in Scala syntax, instead of obscure C++ templates.
You can try enableIf.scala 

在 2013年3月4日星期一 UTC+8下午11:24:17,Hossein写道:
Dear all,

In C++, there are famous SFINAE [1] techniques which people have
widely misused for years to get things like enable_if<> and
disable_if<> [2]. My humble summary of what the two latter creatures
do is that they enable/disable certain features upon certain
compile-time checks. Here is an example of when I've been in such a
need over my recent Scala developments:

class B
class D1 extends class B
class D2 extends class B

trait BUser[TheB <: B] {
  def f1: TheB => Int//only applicable to D1
  def f2: TheB => Unit

class D2User extends BUser[D2] {
  override def f1: D2 => Int = throw new ...
  override def f2...

That is, I throw an exception when f1 is used for D2s. This prevents
things of wrong semantics of course, but, that's just too late! Is
there anyway I can keep the BUser as a common interface and still have
its f1 automatically disabled at the appropriate time so that the
compiler would even stop the implementer of D2User if they mistakenly
choose to implement f1? In case that wasn't entirely clear, I am
requesting for something which acts at compile-time as opposed to



Please don't turn this into a Scala vs C++ thread -- I'm not going to
participate. I am simply referring to pieces of C++ that I've found
myself in need of in Scala. Feel free to question my motivations for
wanting this extra protection. But, please don't remind me of the
harms bringing old habits into a new language may have. I really think
I am asking for something legitimate in this very instance.


[1] <a href="" target="_blank" rel="nofollow" onmousedown="this.href=&#39;\\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFz4c94HK-cAxjYKjXko5Z7Wnn0RQ&#39;;return true;" onclick="this.href=&#39;\\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFz4c94HK-cAxjYKjXko5Z7Wnn0RQ&#39;;return true;">
[2] <a href="" target="_blank" rel="nofollow" onmousedown="this.href=&#39;\\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG3jiNYasjyhnqQoxVW69W7gdFRPA&#39;;return true;" onclick="this.href=&#39;\\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG3jiNYasjyhnqQoxVW69W7gdFRPA&#39;;return true;">


Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - <a href="" target="_blank" rel="nofollow" onmousedown="this.href=&#39;\\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGBVEOlZ6cy05svElm3FxxVI10J9w&#39;;return true;" onclick="this.href=&#39;\\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGBVEOlZ6cy05svElm3FxxVI10J9w&#39;;return true;">

You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit