The Artima Developer Community
Sponsored Link

Programming in Scala Forum
How far can I expect to take implicit conversions?

1 reply on 1 page. Most recent reply: Dec 26, 2007 11:40 PM by Rob Lally

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Randolph Kahle

Posts: 6
Nickname: randykahle
Registered: Apr, 2006

How far can I expect to take implicit conversions? Posted: Dec 25, 2007 5:47 AM
Reply to this message Reply
Advertisement
I am intrigued by the idea that the Scala compiler can detect type mismatches in code and insert implicit type conversions.

I was able to get a simple conversion working that converts java.lang.String to a "StringAspect" (a simple wrapper class) so I can see the utility of this feature and I am thinking I could take this pretty far in order to simplify code structures.

But ... how far can I expect to take this?

For example, would it be reasonable to convert a java.lang.String containing XML text to a DOM? If I did, what if the conversion throws an exception because the XML text was not properly formed?


Rob Lally

Posts: 7
Nickname: roblally
Registered: Dec, 2007

Re: How far can I expect to take implicit conversions? Posted: Dec 26, 2007 11:40 PM
Reply to this message Reply
My personal take on implicit conversions is that they should only be used when the conversion is guaranteed to succeed.

I wouldn't rely on an implicit conversion from a String to an XML DOM object unless the String was a constant/literal that I controlled; adding a point of failure that doesn't even have a visible representation in the code is just asking for trouble.

My rule of thumb is to use them when they make the code clearer not just to make the code shorter. This restricts their use to occasions where you are dealing with objects that reside at the boundaries of two type hierarchies.

Flat View: This topic has 1 reply on 1 page
Topic: Chapter 3: p58 Previous Topic   Next Topic Topic: book, reversing

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use