# How do castfn 's work?

**URL:** <https://discourse.ats-lang.org/t/how-do-castfn-s-work/342>\
**Category:** Google Group\
**Created:** [February 26, 2014, 3:36pm UTC](https://discourse.ats-lang.org/t/how-do-castfn-s-work/342 "2014-02-26T15:36:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [February 26, 2014, 3:36pm UTC](https://discourse.ats-lang.org/t/how-do-castfn-s-work/342/1 "2014-02-26T15:36:00Z")

</div>

The following example typechecks and compiles, which makes me think that  
certain castfns are implemented behind the scenes (like those that may be  
well-defined in native C):

// test float to double and double to float

extern  
castfn  
double\_to\_float  
(x: double): float

extern  
castfn  
float\_to\_double  
(x: float): double

implement main0 () =  
{  
val x: double = 7.0  
val xf:float = double\_to\_float(x)  
val xd: double = float\_to\_double(xf)  
// val xd2: double = xf // sensibly doesn’t typecheck  
}

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [February 26, 2014, 5:09pm UTC](https://discourse.ats-lang.org/t/how-do-castfn-s-work/342/2 "2014-02-26T17:09:05Z")

</div>

Yes, this only works for generating C code from ATS source.On Wednesday, February 26, 2014 10:36:00 AM UTC-5, Brandon Barker wrote:

> The following example typechecks and compiles, which makes me think that  
> certain castfns are implemented behind the scenes (like those that may be  
> well-defined in native C):
> 
> // test float to double and double to float
> 
> extern  
> castfn  
> double\_to\_float  
> (x: double): float
> 
> extern  
> castfn  
> float\_to\_double  
> (x: float): double
> 
> implement main0 () =  
> {  
> val x: double = 7.0  
> val xf:float = double\_to\_float(x)  
> val xd: double = float\_to\_double(xf)  
> // val xd2: double = xf // sensibly doesn’t typecheck  
> }
