views:

1119

answers:

7

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.

+18  A: 

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 ()
Dario
I edited my question a bit to clarify.
usr
Lets say i have built a logger that wants to log everything to a text file except objects of type customer that are going to be logged to a database. I want the logging function to carry the burden of checking. I don't want the caller to call a different method depending on the object type. How the logging is being done is an implementation detail.
usr
A typeclass does what i want. Nice.
usr
+2  A: 

In C# it's likely more efficient not to do the cast:

return x == null ? null : x.ToString();

In Haskell there's the "show" type class, anything that implements the type class can have show called on it and so no casting is needed.

Tom
+3  A: 

If you want to have objects that can either be Customers or Orders, then you'd introduce a new datatype "Customer|Order" so that each object carries a type tag saying which it is.

I don't know the Haskell syntax offhand, but in F# it would be

type CustOrOrd =
    | Cust of Customer
    | Ord of Order

let F (x:CustOrOrd) : int =
    match x with
    | Cust c -> 1
    | Ord o -> 2

let thing1 = Cust someCustomer
let thing2 = Ord someOrder
// can call F on thing1 or thing2

More generally, in Haskell if you want to do something like a fixed OO type hierarchy (for a fixed set of highly related types, to be able to treat them as the same type in some cases), you may use a union type as above. On the other hand, for common operations across a variety of unrelated types (like ToString/Show) you might use a Haskell type class.

Brian
+3  A: 

This is something I've tried before, and I don't think you can do it.

Introduction to Haskell Pure Functions

Haskell takes a very different approach. It starts by performing type analysis with a type checker to understand your program at compile time. The type checker strictly prohibits type casting and does not allow type errors to be ignored. Because types are checked at compile time, and there is no escape from the type checker, Haskell is frequently described as both statically typed and strongly typed.

Farley Knight
+4  A: 

Dario's right, generally in Haskell you want to create a type class to dispatch on something's type. That being said, there is a type safe way to cast in Haskell. This is part of the Scrap your boilerplate generic programming library that allows you to write the "interesting" parts of manipulations of complex nested data types while SYB fills in the blanks. Brain meltingly awesome. Here's a presentation on it in ppt or html.

Here's what the cast looks like:

cast :: (Typeable a, Typeable b) => a -> Maybe b
ghci> (cast 'a') :: Maybe Char
Just 'a'
ghci> (cast 'a') :: Maybe Bool
Nothing
ghci> (cast True) :: Maybe Bool
Just True
Kobold
I upvoted this because it would also be a solution.
usr
+1  A: 

Casting and is/instanceof make sense in languages that feature subtyping. Your question can be answered in several ways:

  • Haskell does not have subtyping and so is/instanceof make no sense in Haskell; there are only explicit conversions. There is no object type from which all types extend.
  • Haskell's type classes offer ad-hoc polymorphism and help in overloading a single name for many conversions. For example (as others have pointed out) show is the overloaded name for converting any type to a String. Type classes can also be used to make certain conversions (seem) implicit.
  • Using type classes, the authors of the generic Scrap Your Boilerplate library have created a safe cast function (also pointed out by others).
Martijn
A: 

The most straightforward way is to use Data.Typeable, as Dario observes, which covers all the standard types, though you need to pass "Deriving Typeable", or otherwise implement typeOf for your own type definitions to be covered. This is not standard Haskell 98, but it is in ghc since 6.2.2.

Your code could be realised:

 stringtyperep :: TypeRep
 stringtyperep = typeOf "somestring"

 F :: (Typeable 'a) =>'a -> Integer
 F x | (typeOf x == stringtyperep) = 1
 F x = 2

In general, OO-style type reflection is better done with generic programming, but this would not be a good example for that.

Additionally, all types in typeclass Typeable can be "cast" into Data.Dynamic.

Charles Stewart