# Overloading the \`int\` intrinsic function to take \`logical\` inputs

**URL:** <https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422>\
**Category:** Help\
**Created:** [December 20, 2021, 10:05am UTC](https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422 "2021-12-20T10:05:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [December 20, 2021, 10:05am UTC](https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422/1 "2021-12-20T10:05:21Z")

</div>

I would like to overload the `int` intrinsic function so that `int(.true.) = 1` and `int(.false.) = 0`. Here is my implementation. Is it correct and standard-compliant? I have never tried overloading intrinsic functions. Thank you for suggestions and comments.

```auto
module int_mod
implicit none
private
public :: int

interface int                                                                                                                        
      module procedure logical_to_int                                                                                                  
end interface int 

contains 

pure elemental function logical_to_int(x) result(y)                                                                                  
implicit none                                                                                                                        
logical, intent(in) :: x                                                                                                             
integer :: y                                                                                                                     
y = merge(tsource=1, fsource=0, mask=x)                                                                                        
end function logical_to_int           

end module int_mod                                                                                                         

```

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [December 20, 2021, 5:28pm UTC](https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422/2 "2021-12-20T17:28:42Z")

</div>

Yes, overloading intrinsic functions is standard compliant. An example from one of my libraries:

> <https://gitlab.com/everythingfunctional/quaff/-/blob/main/src/angle_m.f90#L206>

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [December 21, 2021, 1:40am UTC](https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422/3 "2021-12-21T01:40:33Z")

</div>

Why some of these are not standard is puzzling; but some other interesting ones are to allow  
int() and real() to take numeric strings as well as logicals, to allow using == and and /= with logicals instead of .EQV. and .NEQV., too change SIGN so it only requires a single value, allow merge() to take scalar strings of different lengths, and to allow the // operator to take numeric values and convert them to strings.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [December 21, 2021, 2:14am UTC](https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422/4 "2021-12-21T02:14:27Z")

</div>

I totally agree that it would be nice to have `int` and `real` taking strings and logical values intrinsically. Regarding `==` and `/=`, it is a bit unexpected to me that they do not handle logicals, even though strings are accepted.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [December 21, 2021, 2:57am UTC](https://fortran-lang.discourse.group/t/overloading-the-int-intrinsic-function-to-take-logical-inputs/2422/5 "2021-12-21T02:57:18Z")

</div>

Several compilers allow == and =/ with logicals, but it has never been standardized. There have been several discussions regarding this and some dusty corners regarding operator precedence that I was trying to find to link to, but did not find them with a cursory scan. Functions for converting between numeric values and strings is probably the most “reinvented wheel” in Fortran, and I have felt extending int() and real() was a natural place for them, but there are some issues about what to do with error conditions; but there are some of the same issues with overflow (like doing an int(too\_big\_a\_float\_to\_fit\_in\_the\_integer\_kind) that are not exactly handled gracefully either with those same functions.

A few compilers supply the missing “CHARACTER()” function to convert numeric values to strings too, which seems a nice name for such a function.
