From fc1f78d8fad7303cf9cfb0c00399a479ff8fbd17 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 12 Nov 2024 15:13:06 +0100 Subject: [PATCH] FAQ: tweak answer about this.type this came up on Discord. a user only needed to return the same instance, so there was no need to send them down other paths besides `this.type` --- _overviews/FAQ/index.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/_overviews/FAQ/index.md b/_overviews/FAQ/index.md index 7c0b101dd2..e731fbda9c 100644 --- a/_overviews/FAQ/index.md +++ b/_overviews/FAQ/index.md @@ -292,14 +292,16 @@ For more details, see the Scala Style Guide, [here](https://docs.scala-lang.org/ ### How can a method in a superclass return a value of the “current” type? -First, note that using `this.type` won't work. People often try that, -but `this.type` means "the singleton type of this instance", a -different and too-specific meaning. Only `this` itself has the -type `this.type`; other instances do not. - -What does work? Possible solutions include F-bounded polymorphism -_(familiar to Java programmers)_, type members, -and the [typeclass pattern](http://tpolecat.github.io/2013/10/12/typeclass.html). +Using `this.type` will only work if you are returning `this` itself. +`this.type` means "the singleton type of this instance". Only `this` +itself has the type `this.type`; other instances of the same class do +not. + +What does work for returning other values of the same type? + +Possible solutions include F-bounded polymorphism _(familiar to Java +programmers)_, type members, and the [typeclass +pattern](http://tpolecat.github.io/2013/10/12/typeclass.html). This [blog post](http://tpolecat.github.io/2015/04/29/f-bounds.html) argues against F-bounds and in favor of typeclasses;