Skip to content

Example accessing enum companion #14142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2022
Merged

Conversation

som-snytt
Copy link
Contributor

Fixes #11249

@som-snytt
Copy link
Contributor Author

For "future reference", so to speak.

object ExoPlanetaryData:
  final val VulcanMass   = 5.331e+24
  final val VulcanRadius = 6.15297e6
import ExoPlanetaryData.*

import Planet.{VenusMass as VM, *}
enum Planet(mass: Double, radius: Double):
  private final val (MercuryMass @ _, MercuryRadius @ _) = (3.303e+23, 2.4397e6)

  case Mercury extends Planet(MercuryMass, MercuryRadius)             // Not found
  case Venus   extends Planet(VM, VenusRadius)                        // illegal reference
  case Earth   extends Planet(Planet.EarthMass, Planet.EarthRadius)   // ok
  case Vulcan  extends Planet(VulcanMass, VulcanRadius)               // ok
object Planet:
  private final val (VenusMass @ _, VenusRadius @ _) = (4.869e+24, 6.0518e6)
  private final val (EarthMass @ _, EarthRadius @ _) = (5.976e+24, 6.37814e6)
end Planet

@bishabosha bishabosha force-pushed the doc/enum-example branch 3 times, most recently from 7440220 to bcaedf9 Compare February 23, 2022 16:28
@bishabosha
Copy link
Member

bishabosha commented Feb 23, 2022

@som-snytt please would you be able to check the commit I pushed?

@som-snytt
Copy link
Contributor Author

@bishabosha I see you replaced royal "we"? I assumed you were required to use "royal we" as a condition of citizenship. But I am not bound to convention.

@bishabosha
Copy link
Member

I see you replaced royal "we"? I assumed you were required to use "royal we" as a condition of citizenship. But I am not bound to convention.

I've noticed I have actually already used "we" in some other parts of this document (like for deprecation of enum cases) 😂

@som-snytt
Copy link
Contributor Author

som-snytt commented Feb 23, 2022

I will leave it to your discretion.

I recently joked that so-and-so should just change their pronouns to "we". I've forgotten the context, as one ought to do when there is no implicit context.

Edit: OK, now I see that the other usage of "we" is actually a deflection: No one wishes to be responsible for demoting Pluto, so it is natural to spread or diffuse the blame among the group.

@bishabosha bishabosha merged commit e2f32fd into scala:main Feb 23, 2022
@som-snytt som-snytt deleted the doc/enum-example branch February 23, 2022 19:19
@oscar-broman
Copy link

Is it really necessary to use that pattern matching syntax? The example would look a bit simpler if the names started with lowercase letters.

I'm just thinking it might confuse novices reading the documentation, especially since enums is a simple concept that people will want to learn initially as they learn a new language.

@som-snytt
Copy link
Contributor Author

@oscar-broman I don't mind following up if you suggest a delta. I'm not sure what you mean, as it has been a few months.

I'm not sure the text is intended for novices? I mean, I can barely follow it.

@oscar-broman
Copy link

I just meant the example could look like this instead:

  import Planet.*
  enum Planet(mass: Double, radius: Double):
    private final val (mercuryMass, mercuryRadius) = (3.303e+23, 2.4397e6)

    case Mercury extends Planet(mercuryMass, mercuryRadius)             // Not found
    case Venus   extends Planet(venusMass, venusRadius)                 // illegal reference
    case Earth   extends Planet(Planet.earthMass, Planet.earthRadius)   // ok
  object Planet:
    private final val (venusMass, venusRadius) = (4.869e+24, 6.0518e6)
    private final val (earthMass, earthRadius) = (5.976e+24, 6.37814e6)
  end Planet

@bishabosha
Copy link
Member

I'll open a PR

@som-snytt
Copy link
Contributor Author

@oscar-broman Thanks for clarifying. The syntax was "pedagogical" because normally we expect "constants" to be uppercase. The idiom shown is how to achieve that.

@som-snytt
Copy link
Contributor Author

@bishabosha I'm agnostic as to whether the example is done the right way or the wrong way. :)

@bishabosha
Copy link
Member

see #14556

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

document enum cases cannot use private val as part of definition
3 participants