On Tue, Jul 08, 2008 at 10:02:06PM -0700, Jim McBeath wrote:
> As a vehicle for helping me learn Scala, I have converted one of my
> Java programs over to Scala. The Java code included a class with two
> methods overloading the same name, one with an optional File argument
> and the other with an optional String argument (representing a file
> name).
Thanks for the suggestions.
Tony Morris wrote:
> Personally, I'd delete the String function and write a String -> File
> implicit (if that is not to your liking, I'd abstract it in away such that
> it does).
The string I pass in may be a name without a path component,
to be turned into a File using a base directory in the object,
so I don't think I can define this implicit conversion.
> These two functions exists in Scalaz
>
http://projects.workingmouse.com/public/scalaz/artifacts/latest/scaladoc/scalaz/Maybe.html#toNull>
http://projects.workingmouse.com/public/scalaz/artifacts/latest/scaladoc/scalaz/Option$object.html#onull(A)
I'm happy to hear that there is a standard solution, but for now I
think I will stick to my little object rather than add a dependency
to sbaz. It would be nice if the above-referenced doc pages had
a one-line description of what each method does. Unfortunately,
I am not familiar enough with the terminology used in those classes
to understand what all of the methods do based solely on their
names and signatures. I will look at the source code if I reach
the point of really wanting to know.
> This discussion has been had. The purpose of Option is not to have null
> floating around. It just solves that problem by consequence. Similarly,
> the purpose of a car is not to generate heat from its engine bay. Option
> is just an algebra like so many others.
Thanks for pointing this out, I will see if I can find these
discussions in the archives.
Viktor Klang wrote:
> def foo(s:String, f:Either[File,String]):Unit = { }
> And then you can match on it.
An interesting suggestion, but not quite usable for my situation.
I want to be able to take an Option value returned by another method
that is interfacing to a legacy Java method, and pass that directly
back to my method.
The combination of the suggestions about using implicit conversions
and Either got me thinking about how I might be able to use those
two together to do what I want. I have started playing around with
various implicit conversions and combinations of Option and Either.
I don't yet have a satisfactory solution, but in any case it is
helping me with my other goal of learning the language and concepts.
The learning is more important to me than the solution for this
particular problem.
And I am enjoying the continuing discussion about Strings and
Files. :-)
--
Jim