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

Namespacing issues with Down "Node" type #242

Closed
djstevenson opened this issue Jan 23, 2021 · 8 comments
Closed

Namespacing issues with Down "Node" type #242

djstevenson opened this issue Jan 23, 2021 · 8 comments
Labels
workaround-available There is a workaround available

Comments

@djstevenson
Copy link

djstevenson commented Jan 23, 2021

I want to build an Html object using Pointfree's https://github.com/pointfreeco/swift-html
and this Down package.

The idea was to use Down to parse Markdown, and to use a "visitor" (as you define in DebugVisitor for testing) - as I visit a node, I generate the corresponding Html node (so, rather than doing Markdown -> String, I'm trying to do Markdown -> [Html.Node])

I have a problem with the name spacing - both Html and Down define a Node type. That's normal - I can specifically refer to their type as Html.Node - but I can't refer to your type as Down.Node

My struct imports both:

import Down
import Html

Then, a reference to "Node" produces the expected error "'Node' is ambiguous for type lookup in this context", along with:

1. Found this candidate (Down.Node)
2. Found this candidate (Html.Node) 

If I refer to an html type as Html.Node all is fine. But, despite Swift saying "Down.Node" is a candidate, when I use Down.Node I get

   private func nodeWithChildren(_ node: Down.Node, value: String) -> String {     **x 'Node' is not a member type of 'Down'**

I can't seem to get the Down node type namespaced.

Xcode 12.3
Html: 0.3.1 https://github.com/pointfreeco/swift-html
Down: 0.9.4

(Note, I'm relatively new to Swift, so sorry if I'm just being a bit dumb. If so, any pointers towards being less dumb would be gratefully received!)

@djstevenson
Copy link
Author

Obviously, my workaround is to render Markdown to a String, then include that as 'raw' in my Pointfree Html struct. But I was kinda hoping to get the full safety etc of their library while writing some content in Markdown.

@djstevenson
Copy link
Author

djstevenson commented Jan 23, 2021

@johnxnguyen
Copy link
Owner

johnxnguyen commented Jan 23, 2021

Hey @djstevenson , thanks for getting in touch and for the nice example repo. So this is an interesting problem that I've never come across before. The source of the problem is that the Down module contains a public type called Down which is confusing Xcode.

Consider the following code:

// Down is the module name
import Down

// You'd think that Down here is referring to the module, but it's actually
// referring to a struct called Down within the Down module. If you cmd + click
// on the word Down you'll see the definition of the struct.
typealias DownNode = Down.Node

You're getting the error "Node is not a member type of Down" because Xcode sees that the struct called Down doesn't have a nested type called Node.

Unfortunately I couldn't figure out a way for Xcode to refer to the module rather than the struct, without having to change the name of the Down struct. The good news is that you can easily solve the problem by declaring your typealiases for Down and Html in separate files, such as:

// In DownAliases.swift
import Down

typealias DownNode = Node
typealias DownText = Text
typealias DownImage = Image
...


// In HtmlAliases.swift
import Html

typealias HtmlNode = Node
...

Then you can use those aliases in any other file in your module, even files that import Down and / or Html.

Try it out and let me know how you go. And lastly, I don't believe there are stupid questions so don't hesitate to ask any more that you might have, I'm happen to help in any way I can.

@johnxnguyen johnxnguyen added the workaround-available There is a workaround available label Jan 23, 2021
@djstevenson
Copy link
Author

djstevenson commented Jan 23, 2021

Thank you for the lightning quick response!

Unfortunately, I'm still seeing the same error in DownAliases.swift - I've pushed a commit implementing your suggestion to my demo repo.

Searching Swift forums and StackOverflow have led me to the conclusion this is a known problem in the current version of Swift, being unable to disambiguate package from type when they are named the same.

djstevenson added a commit to djstevenson/Down-issue-242 that referenced this issue Jan 23, 2021
@djstevenson
Copy link
Author

I think I might have cracked it, with DownAliases.swift containing this, which I now realise is probably what you meant to put in your workaround example above:

// In DownAliases.swift
import Down

typealias DownNode = Node
typealias DownText = Text
typealias DownImage = Image

djstevenson added a commit to djstevenson/Down-issue-242 that referenced this issue Jan 23, 2021
@johnxnguyen
Copy link
Owner

johnxnguyen commented Jan 23, 2021

@djstevenson ahhhh yeah, sorry my bad! Indeed I mistyped the code block example, I'll update the comment and close this issue. Feel free to reach out for anything else!

@djstevenson
Copy link
Author

I really appreciate your help, thank you.

@kengruven
Copy link
Collaborator

Looks like SR-898 -- a known language limitation for 5 years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
workaround-available There is a workaround available
Projects
None yet
Development

No branches or pull requests

3 participants