# Class interface with parameters?

**URL:** <https://discuss.ocaml.org/t/class-interface-with-parameters/1584>\
**Category:** Learning\
**Created:** [February 16, 2018, 10:34pm UTC](https://discuss.ocaml.org/t/class-interface-with-parameters/1584 "2018-02-16T22:34:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rauschma](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rauschma/32/730_2.png) [@rauschma](https://discuss.ocaml.org/u/rauschma)\
**Post date:** [February 16, 2018, 10:34pm UTC](https://discuss.ocaml.org/t/class-interface-with-parameters/1584/1 "2018-02-16T22:34:38Z")

</div>

Is there a way to define an interface for the following class?

```auto
class point x y =
  object (self)
    method dist = sqrt ((x *. x) +. (y *. y))
    method toString =
      "(" ^ (string_of_float x) ^ (string_of_float y) ^ ")"
  end

```

If it didn’t have parameters, the class interface would be:

```auto
class type pointType =
  object
    method dist : float
    method toString : string
  end

```

If I try to add parameters analogously to the following type that utop infers, I get a syntax error.

```auto
class point : float -> float ->
  object
    method dist : float
    method toString : bytes
  end

```

---

<div class="post-metadata">

**Author:** ![grayswandyr](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/grayswandyr/32/94_2.png) [@grayswandyr](https://discuss.ocaml.org/u/grayswandyr)\
**Post date:** [February 19, 2018, 8:26am UTC](https://discuss.ocaml.org/t/class-interface-with-parameters/1584/2 "2018-02-19T08:26:50Z")

</div>

Looking at the manual, in particular the syntax of `class type` definitions, what you ask for doesn’t seem possible. `class type` only introduces an abbreviation for an `object ... end` part. Notice that the class and object layer of OCaml may seem exotic to newcomers used to more mainstream OO languages.  
What did you want to achieve?

---

<div class="post-metadata">

**Author:** ![robkuz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/robkuz/32/842_2.png) [@robkuz](https://discuss.ocaml.org/u/robkuz)\
**Post date:** [February 19, 2018, 3:01pm UTC](https://discuss.ocaml.org/t/class-interface-with-parameters/1584/3 "2018-02-19T15:01:01Z")

</div>

what about

```
type pointType = < dist : float; toString : string>

```

?

---

<div class="post-metadata">

**Author:** ![rauschma](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rauschma/32/730_2.png) [@rauschma](https://discuss.ocaml.org/u/rauschma)\
**Post date:** [February 24, 2018, 4:50pm UTC](https://discuss.ocaml.org/t/class-interface-with-parameters/1584/4 "2018-02-24T16:50:32Z")

</div>

I was experimenting with making instance variables truly private by hiding them from subclasses. If you can’t include the parameters in the type then that’s slightly more complicated.

---

<div class="post-metadata">

**Author:** ![rauschma](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rauschma/32/730_2.png) [@rauschma](https://discuss.ocaml.org/u/rauschma)\
**Post date:** [February 24, 2018, 4:51pm UTC](https://discuss.ocaml.org/t/class-interface-with-parameters/1584/5 "2018-02-24T16:51:50Z")

</div>

I was looking for something specific: a type for a class. Your solution would work as the type of `self`, though.
