Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Haskell's type system is very different and quite a bit smarter then C/C++/Javas. To understand why you are not going to get the answer you expect, it will help to compare the two.</p> <p>For C and friends the type is a way of describing the layout of data in memory. The compiler does makes a few sanity checks trying to ensure that memory is not corrupted, but in the end its all bytes and you can call them what ever you want. This is even more true of pointers which are always laid out the same in memory but can reference anything or (frighteningly) nothing.</p> <p>In Haskell, types are a language that one writes to the compiler with. As a programmer you have no control over how the compiler represents data, and because haskell is lazy a lot of data in your program may be no more then a promise to produce a value on demand (called a <code>thunk</code> in the code GHC's and HUGS). While a c compiler can be instructed to treat <em>data</em> differently, there is no equivalent way to tell a haskell compiler to treat one type like another <em>in general</em>.</p> <p>As mentioned in other answers, there are some types where there are obvious ways to convert one type to another. Any of the numerical types like Double, Fraction, or Real (in general any instance of the <code>Num</code> class) can be made from an <code>Integer</code>, but we need to use a function specifically designed to make this happen. In a sense this is not a 'cast' but an actual function in the same way that <code>\x -&gt; x &gt; 0</code> is a function for converting numbers into booleans.</p> <p>I'll make one last guess as to why you might be asking a question like this. When I was just starting with haskell I wrote a lot of functions like:</p> <pre><code>area :: Double -&gt; Double -&gt; Double -- find the area of a rectangle area x y = x * y </code></pre> <p>I would then find myself dumping <code>fromInteger</code> calls all over the place to get my data in the correct type for the function. Having come from a C background I wrote all my functions with monomorphic types. The trick to not needing to cast from one type to another is to write functions that work with different types. Haskell type classes are a huge shift for OOP programmers and so they often get ignored the first couple of tries, but they are what make the otherwise very strict haskell type system usable. If you can relax your type signatures (e.g. <code>area :: (Num a)=&gt; a -&gt; a -&gt; a</code>) you will find yourself wishing for that cast function much less often.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload