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

Opaque Types in Book #2158

Closed
bblfish opened this issue Aug 18, 2021 · 6 comments
Closed

Opaque Types in Book #2158

bblfish opened this issue Aug 18, 2021 · 6 comments

Comments

@bblfish
Copy link
Contributor

bblfish commented Aug 18, 2021

https://github.com/scala/docs.scala-lang/blob/main/_overviews/scala3-book/types-opaque-types.md

That page is very nicely written and helpful.
Reading it does make me wonder if one looses the efficiency gains if one combines a bit of the abstraction with the opaque types.

For example it seems like it could still make sense to have a trait Logarithm with a type Logarithm and then have a couple of specialisations: one for Double and one for BigInt perhaps, ...

We have a library like that in Banana-RDF that used Projection types (see description here) which are deprecated and so I it could be that we want a way to replace those with opaque types. Note that we did have a leakage problem in banana-rdf.

@julienrf
Copy link
Contributor

Hi @bblfish!

I don’t think this issue tracker is the best place to discuss your problem. Maybe open a thread on users.scala-lang.org?

The difference between opaque types and abstract type members is that abstract type members can have several “concretizations” (whereas opaque types alias to one thing only). Abstract type members seem to be what you are looking for, from what I understand. You might be interested in reading/watching the following resources:

I hope that helps!

I am closing this issue, I suggest you open a discussion thread on users.scala-lang.org instead if you want to continue.

@bblfish
Copy link
Contributor Author

bblfish commented Aug 18, 2021

Hi @julienrf, I agree that the larger question may not fit the place of the discussion for the book. I just added that as context for your interest.

But the narrower question - re the Logarithm - does seem very relevant to the book.
There has to be a simple answer: either adding the bit of abstraction looses the efficiency gains of opaque types or not.

  • If not, then it would be worth stating that there
  • If it does loose efficiency, then it is also important to mention, as those types of things are very difficult to test.

Here is some code that compiles

trait Logarithm {
	type Log
}

object Logarithm extends Logarithm {
	opaque type Log = Double
}

object BILog extends Logarithm {
	opaque type Log = BigDecimal
}

@julienrf
Copy link
Contributor

OK, thank you for clarifying. I think the documentation already answers your question. With abstract type members, there is no cost of “wrapper” (unlike the first solution shown in that page), but there can be a boxing cost (see https://github.com/scala/docs.scala-lang/blob/main/_overviews/scala3-book/types-opaque-types.md#boxing-overhead)

@bblfish
Copy link
Contributor Author

bblfish commented Aug 18, 2021

ok, thanks for all the pointers. I'll be checking them out soon, as I have to get round to upgrading that library.

@SethTisue
Copy link
Member

If you see an opportunity for improving the book text, a pull request would be welcome.

@bblfish
Copy link
Contributor Author

bblfish commented Aug 18, 2021

I guess it would be helpful if you could be extra clear that the code above is problematic (ie. that opaque types don't give the desired benefits in inherited traits as shown. )

Because I have a feeling that I would not know how to test that myself.

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

No branches or pull requests

3 participants