This page is no longer maintained — Please continue to the home page at

Question on implicit conversions/type inference from method calls.

No replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
To begin with, I had a case where I wanted to implicitly convert strings into Text nodes (roughly equivalent to HTML nodes containing nothing but text). This was easy, and let me do things like:
new Branch += ("Hello", "Goodbye"), where the strings are automatically converted to the Text instances needed by Branch.
Next up, I actually had two "common-case" types of texts nodes; one was editable, and the other was fixed, uneditable by the user. The latter was represented by class FixedText, and I simply added a method .fix to the Text class that would take the initial text for a Text instance, and return a new FixedText with the same initial text. So, then:
new Branch += ("Hello", "Goodbye".fix)

But you're probably well ahead of me and realize why this failed. ".fix" was invoked on "Goodbye" _before_ the implicit conversion of "Goodbye" to a Text instance. String doesn't understand ".fix", so no go.
Now in some sense, one could argue that the type inferencer _could_, upon failure of .fix on a String, look for classes that has .fix defined and then see if there is a conversion from String to such a class--but I can understand why that's not done. Without thinking deeply at all upon the topic, I can think of enough cases that would have to be handled to chill my blood.
So my question is simply; given the basics of the situation outlined above, is there a _nice_ (elegant, doesn't require conversions that really shouldn't be done, etc.) way of concisely denoting Text and FixedText nodes. Currently I'm using a local function definition "fix" so I write:
new Branch += ("Hello", fix("Goodbye"))

and I'm OK with that--I'd prefer it to abusing Scala capabilities. However, both fixed and editable text will be common on the programs written using this API, so it would be nice to write something similar to
new Branch += ("Hello", ~"Goodbye")

if I could do it _nicely_

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland