# Creating a getter of a class with a type parameter

**URL:** <https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727>\
**Category:** Question\
**Created:** [June 22, 2019, 1:13am UTC](https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727 "2019-06-22T01:13:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael](https://users.scala-lang.org/user_avatar/users.scala-lang.org/michael/32/1090_2.png) [@Michael](https://users.scala-lang.org/u/Michael)\
**Post date:** [June 22, 2019, 1:13am UTC](https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727/1 "2019-06-22T01:13:32Z")

</div>

```
indent preformatted text by 4 spaces

```

I can’t get this example to work, and I don’t know why.

```auto
class ClassExample[T](private var _value: T) {
  def value = _value
  def value_ (newValue: T): Unit = _value = newValue
}

class UseExample {
  val _classExample = new ClassExample(false:Boolean)

  def getCE: ClassExample[Boolean] = _classExample // here is where I get the error below
}

The error is "Expression of type Boolean => ClassExample[Boolean] doesn't conform to expected type ClassExample[Boolean]"

What am I doing wrong?
```

---

<div class="post-metadata">

**Author:** ![tyohDeveloper](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tyohdeveloper/32/1094_2.png) [@tyohDeveloper](https://users.scala-lang.org/u/tyohDeveloper)\
**Post date:** [June 22, 2019, 5:38am UTC](https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727/2 "2019-06-22T05:38:37Z")

</div>

I’m just a scala beginner. My S/N may not be high.

• The first line in UseExample doesn’t refer to ClassExample[Boolean] rather the raw trait.

I think this is the root of the problem.

Scala has specific syntactical use of underscores. I have yet to use it myself. But it does give me the willies to see them used as elements in identifiers. I know it’s legal. The brief style guide on [scala-lang.org](http://scala-lang.org) speaks to the issue. Me? I decided to look for other places I could mess up.

---

<div class="post-metadata">

**Author:** ![siddhartha-gadgil](https://users.scala-lang.org/user_avatar/users.scala-lang.org/siddhartha-gadgil/32/46_2.png) [@siddhartha-gadgil](https://users.scala-lang.org/u/siddhartha-gadgil)\
**Post date:** [June 22, 2019, 6:28am UTC](https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727/3 "2019-06-22T06:28:43Z")

</div>

I just copy-pasted the code (into an ammonite REPL) and it runs fine.

---

<div class="post-metadata">

**Author:** ![LannyRipple](https://users.scala-lang.org/user_avatar/users.scala-lang.org/lannyripple/32/65_2.png) [@LannyRipple](https://users.scala-lang.org/u/LannyRipple)\
**Post date:** [June 22, 2019, 3:52pm UTC](https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727/4 "2019-06-22T15:52:01Z")

</div>

The code you have is correct but not idiomatic. Scala knows that `false` is Boolean so what you have done with `new ClassExample(false:Boolean)` is widen the value to type Boolean, which is a no-op in this case. Just `new ClassExample(false)` is sufficient.

I’m having no problems pasting this into the REPL so I think the problem you are experiencing is local to your environment.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [June 22, 2019, 4:06pm UTC](https://users.scala-lang.org/t/creating-a-getter-of-a-class-with-a-type-parameter/4727/5 "2019-06-22T16:06:18Z")

</div>

> [@tyohDeveloper](#):
>
> Scala has specific syntactical use of underscores.

Well, technically yes – the underscore on its own is a common symbol that I generally pronounce as “whatever” – but it’s totally legal to use it as part of a symbol. I sometimes use an underscore prefix to denote private members, the same way that @Michael does with `_value`; there’s nothing wrong with it, although I wouldn’t say it’s a common idiom.

As for the original problem: my best guess is that the type inference isn’t doing what you expect for some reason. The error suggests that the line

```auto
val _classExample = new ClassExample(false:Boolean)

```

is for some reason defining `_classExample` as a function from `Boolean` to `ClassExample`. I’m not sure _why_ it’s doing that (it’s not what I would expect, and I’m wondering what version of Scala you’re using), but I suspect that changing that to:

```auto
val _classExample: ClassExample[Boolean] = new ClassExample(false)

```

will fix it.

It’s pretty common for newer Scala programmers to rely _way_ more heavily on type inference than experienced ones do. The type inference system is lovely and powerful, but there are lots of edge cases where it doesn’t come out with the answer you expect. In cases where you get a surprising compiler error, one of the first things to try should usually be to spell out your expected types – that will, more often than not, clarify where the problem is, and sometimes just fix it.
