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

seq, array & openArray initialisers broken with polymorphic types #4798

Closed
johnnovak opened this issue Sep 19, 2016 · 1 comment
Closed

seq, array & openArray initialisers broken with polymorphic types #4798

johnnovak opened this issue Sep 19, 2016 · 1 comment

Comments

@johnnovak
Copy link
Contributor

Currently, it is not possible to build a seq, array or openArray of the super type with the initialiser syntax when only a single subtype element is used. It works fine though when at least two different subtypes are passed into the initialiser.

type
  Base = ref object of RootObj
  A = ref object of Base
  B = ref object of Base

# Sequences
var s: seq[Base]

s = @[A(), B()]     # OK
#s = @[A()]         # FAILS TO COMPILE:
                    # Error: type mismatch: got (seq[A]) but expected 'seq[Base]'


# Arrays
var arr: array[0..1, Base]
arr = [A(), B()]    # OK
#arr = [A(), A()]   # FAILS TO COMPILE:
                    # Error: type mismatch: got (array[0..1, A]) but expected 'array[0..1, Base]'


# Open arrays
proc openArrayTest(oa: openArray[Base]) = discard

openArrayTest([A(), B()])   # OK
#openArrayTest([A()])       # FAILS TO COMPILE:
                            # Error: type mismatch: got (array[0..0, A])
                            # but expected one of:
                            #   proc openArrayTest(oa: openArray[Base])

openArrayTest(@[A(), B()])  # OK
#openArrayTest(@[A()])      # FAILS TO COMPILE:
                            # Error: type mismatch: got (seq[A])
                            # but expected one of:
                            #   proc openArrayTest(oa: openArray[Base])
@Araq
Copy link
Member

Araq commented Feb 1, 2017

That's not a bug. The subtype relation cannot be lifted from A <: B to C[A] <: C[B] it would be unsound. You need to cast your 'A' to 'Base' explicitly.

@Araq Araq closed this as completed Feb 1, 2017
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

2 participants