Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

haskell - How to type cast

C#:

static int F(object x)
{
 return x is string ? 1 : 2;
}

Haskell? The tricky bit seems to me that Haskell does not have a root type object.

Edited: I do not care about converting to string. I want to know how to typecast (for example to see if an object is a Customer or an Order.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...