# OCaml check whether variable is string

**URL:** <https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103>\
**Category:** Learning\
**Created:** [July 7, 2021, 1:08pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103 "2021-07-07T13:08:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DonCorleone](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/doncorleone/32/2985_2.png) [@DonCorleone](https://discuss.ocaml.org/u/DonCorleone)\
**Post date:** [July 7, 2021, 1:08pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/1 "2021-07-07T13:08:51Z")

</div>

Hello,  
how can I check whether variable is string or not? Is there any function in OCaml which takes one variable as argument and returns true if it’s string and false if not?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [July 7, 2021, 1:38pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/2 "2021-07-07T13:38:22Z")

</div>

Do you mean:

```ocaml
let tautology (x:string) = true

```

More seriously, it doesn’t really make sense to test the type of a variable in OCaml.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [July 7, 2021, 1:45pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/3 "2021-07-07T13:45:03Z")

</div>

The OCaml typechecker does that for you at compile time. There’s no need to do it at runtime. Maybe you can explain your use case though.

---

<div class="post-metadata">

**Author:** ![DonCorleone](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/doncorleone/32/2985_2.png) [@DonCorleone](https://discuss.ocaml.org/u/DonCorleone)\
**Post date:** [July 7, 2021, 1:54pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/4 "2021-07-07T13:54:58Z")

</div>

I want read file line by line and save this lines into the list but I need base case for recursion.  
example of pseudocode:  
let rec savestrinlist file = if (_there must be condition to check if (input\_line file) is not string or char_) then [] else (input\_line file)::savestringinlist file;;

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [July 7, 2021, 2:05pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/5 "2021-07-07T14:05:55Z")

</div>

It might be easier to read the entire file into a string and split the string on newlines into a list of strings. Unless, of course, you don’t want to read the entire file. The size of a file is `in_channel_length ic` and `really_input_string ic` reads it from an input channel `ic`. If you read the file line by line, you need to watch for the end of the file as the base case for the recursion. This is typically signalled by an exception.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [July 7, 2021, 3:02pm UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/6 "2021-07-07T15:02:02Z")

</div>

`input_line in_channel` always gives a string, by definition. So any condition for checking that would always return `true`. The base case for recursion is to handle the `End_of_file` exception that may be thrown by `input_line`. When you hit the end of the file, you know you won’t recurse any more.

---

<div class="post-metadata">

**Author:** ![DonCorleone](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/doncorleone/32/2985_2.png) [@DonCorleone](https://discuss.ocaml.org/u/DonCorleone)\
**Post date:** [July 10, 2021, 7:18am UTC](https://discuss.ocaml.org/t/ocaml-check-whether-variable-is-string/8103/7 "2021-07-10T07:18:27Z")

</div>

yeah, I did same… thank you
