question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Today I decided to waste some time experimenting with virtual types, which is something we’ve discussed often before. A virtual type is a formal member alias belonging to a class or interface, that is refined and assigned a value by concrete subclasses. For example:

interface Foo {
    shared formal alias Type;
    shared formal Type val;
    shared [Type] getIt() => [val];
    shared void takeIt(Type it) {}
}

class Bar() satisfies Foo {
    shared actual alias Type => String;
    val = "hello";
    shared void useIt() {
        print("got: " + getIt().first);
        takeIt(val);
    }
}

Now, this is strictly less powerful than parametric polymorphism, since we don’t have a syntax for expressing refinements, e.g. something like what Scala has, approximately Foo { Type=>String; }. So, when referring to plain Foo, we get a wild-carded Type, analogous to Foo<out Anything> with generics.

On the other hand, looking at some of the code in ceylon-ide-common, it strikes me that this could be a more convenient syntax to handle what look like type families we have going on there.

Anyway, after some experimentation, it doesn’t look completely trivial from a typechecker / model point of view. But it’s certainly not impossible. OTOH, I’m not going to spend any more time on this, at least not for now.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
RossTatecommented, Mar 13, 2016

@sol50 Not true. BETA pioneered virtual types, but there were lots of problems with BETA’s virtual types, in terms of safety, in terms of expressiveness, and in terms of decidability. In fact, virtual types is still an open research area.

Regarding decidability, I believe the restrictions @gavinking imposed makes this form of virtual types decidable. In particular, not allowing refinements prevents virtual types from introducing the infinite digressions that happen with Scala and with wildcards.

0reactions
gavinkingcommented, Mar 13, 2016

Regarding decidability, I believe the restrictions @gavinking imposed makes this form of virtual types decidable. In particular, not allowing refinements prevents virtual types from introducing the infinite digressions that happen with Scala and with wildcards.

Yeah, the system I’m thinking of is something that can be pretty directly re-expressed using parametric polymorphism, so I don’t think it would cause huge problems in terms of decidability.

And I wouldn’t introduce refinements like in Scala. If you need refinements, then regular generics is the thing you should be looking at.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Magento 2 Virtual Type - Mageplaza
On Magento 2 platform, the di.xml supports two types of nodes which are a node type and a node virtualtype , meanwhile virtualtype...
Read more >
Virtual types - Magento Glossary
Virtual types are a way to inject different dependencies into existing PHP classes without affecting other classes and without having to create a...
Read more >
jubnzv/virtual-types.nvim: Neovim plugin that shows ... - GitHub
This plugin shows type annotations for functions in virtual text using built-in LSP client. I use it with ocaml-lsp, but it will probably...
Read more >
Virtual Types, Types, Preferences: Magento 2 Design Patterns ...
The last thing I'd like to cover in this article is virtualType. Virtual types allow us to modify existing classes and inject them...
Read more >
magento2 - What is the difference between type and virtualType
Virtual types are a way to inject different dependencies into existing classes without affecting other classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found