Skip to content
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

Access inner value via RefinedTypeOps extension #120

Merged
merged 3 commits into from
May 26, 2023

Conversation

antonkw
Copy link
Contributor

@antonkw antonkw commented May 22, 2023

No description provided.

@Iltotore
Copy link
Owner

Since value is already used for apply, either etc... I think it would be a better name than raw. Also I think it should return a A :| C to not loose the refinement.

Note that A :| C is already a subtype of A so for an extension like inline def value: A :| C = thisValue.asInstanceOf[A :| C] the following code compiles:

//Assuming `Temperature` is an (opaque) alias for `Double :| Positive`
val temperature: Temperature = Temperature(5)

//Both work
val withRefinement: Double :| Positive = temperature.value
val raw: Double = temperature.value

@antonkw
Copy link
Contributor Author

antonkw commented May 24, 2023

I was inclined to use value but then recalled that value is something that is used as both:

  • a conventional name for lightweight newtypes-like wrappers
  • accessing underlying data (one level down) in e.x. refined library

Hence, to access the underlying IronType (what you've suggested), value is the right name. I was thinking about the equivalent of value.value but it is just not necessary (see below).

Also I think it should return a A :| C to not loose the refinement.

Completely agree. I missed that subtyping makes temperatureX.value + temperatureY.value fully legitimate.

Updated PR to have value:

extension (wrapper: T)
    inline def value: IronType[A, C] = wrapper.asInstanceOf[IronType[A, C]]

Now there is an experience of "unpacking" opaque types (without affecting any previously stated obligations):

val t1 = Temperature(100)
val t2 = Temperature(100)

val result = t1.value + t2.value

@antonkw antonkw changed the title raw Access inner value via RefinedTypeOps extension May 24, 2023
@Iltotore Iltotore merged commit b401ea6 into Iltotore:main May 26, 2023
1 check passed
@Iltotore Iltotore linked an issue May 26, 2023 that may be closed by this pull request
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.

Accessing underlying value of opaque-based iron type
2 participants