In Haskell, all types that allow a conversion to a string instantiate the Show
typeclass which provides
show :: Show a => a -> String
So your whole code is nothing but
f x = show x
or
f = show
with the same generic type f :: Show a => a -> String
(Forall types a
that are conversible to a string, take a value of this type and return a string).
Note that you don't have to do an explicit, run-time type-check like in C#; the generic template is resolved at compile-time. You don't need a polymorphic root type - A cast like in C# would in fact be somewhat complicated and against the language's conception. Instead of allowing arbitrary casts between types, it defined typeclasses for certain meaningful conversions.
Note that compatibility is checked at compile-time:
-- Working
f 1
f "Hallo"
f (1, 2)
f [1, 2, 3]
-- Not working
f (x -> x + 1)
In response to your edited question:
As I said before, arbitrary conversions aren't allowed in Haskell (without very very unsafe code). And since Haskell is not object-oriented, there is no inheritance relationship that required any cast. There simply aren't meaningless object
values that needed runtime-checking/casting. For expressing alternatives, you'll have to define a union type, a typeclass or use the Either
type.
In what case do you encounter an object that is a Customer
or an Order
? A value of that type is simply nonsensical. Please clarify again.
As to your logger example:
You'll need a typeclass:
class Loggable a where
writeToLog :: a -> IO ()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…