Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 817
usr Avatar asked Dec 12 '09 18:12

usr


People also ask

What is cast () in Python?

Prerequisites: Python Data Types. Type Casting is the method to convert the variable data type into a certain data type in order to the operation required to be performed by users.

What is type casting code?

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.

What is type cast operator?

The type cast operator converts the data type of expr to the type specified by type-name : [ type-name ] expr. Explicit type conversions require the type cast operator. Implicit type conversions are performed automatically by 4Test and do not require explicit type casting.

Why do we type cast?

Typecast is a way of changing an object from one data type to the next. It is used in computer programming to ensure a function handles the variables correctly. A typecast example is the transformation of an integer into a string.


2 Answers

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 ()
like image 119
Dario Avatar answered Nov 28 '22 06:11

Dario


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
like image 41
Kobold Avatar answered Nov 28 '22 05:11

Kobold