Skip to content

CLines - Deriving from Array(String) Breaks Unrelated Code in my Project using Array #30

@stellarpower

Description

@stellarpower

Deriving from Standard library types apparently is deprecated (I don't understand the reasons myself) - and the following line defining CLines as deriving from Array(String) means that then Array(T) in the standard library is being deduced to Array(T)+ in external code as part of the same project. This playground link summarises the core of the issue.

This has caused my code to break and not to build when trying to include code requiring Crysterm along with code requiring a different shard. Code internal to the other shard breaks with a confusing message:

Error: expected argument #2 to 'Hash(Char, Array(UInt8).class)#[]=' to be Array(UInt8).class, not Array(UInt8).class

A trivial MRE is available here - note that I don't need to use anything from either library; simply requiring both within the same project causes an error to come from within the osc-crystal library.

I'm coming to Crystal from Ruby originally - and then most of my background is in other languages where deriving from a class in no way is able to affect that class, so personally afraid I'm not understanding what is going on here or why deriving from a type should cause issues with other code that uses this type (in my mind that's surely the basic way inheritance is meant to work). So apologies, but I don't have much I can usefully add in the description here.

But I've been informed that deriving from types in the standard library is probably not good, and that therefore it's worth opening this as an issue here. I'll also be opening a bug for the Crystal compiler itself, as IMO the error message definitely needs some clarification - Array(UInt8) != Array(UInt8) leaves one head-scratching for quite a while.

Potential Solutions:

  • Change CLines to use composition, although I imagine this would involve a significant amount of work in adding boilerplate to proxy functions to what is currently the base class
  • ... (not sure on what else myself, sorry!)

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions