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

Dealing with extra parameters in recursive sumtypes #30

Closed
MilesCranmer opened this issue May 28, 2023 · 5 comments · Fixed by #49
Closed

Dealing with extra parameters in recursive sumtypes #30

MilesCranmer opened this issue May 28, 2023 · 5 comments · Fixed by #49

Comments

@MilesCranmer
Copy link

Hey @MasonProtter, really nice package.

Is there a way I can make a recursive SumType which uses the correct parametrized field type?

For example, say I want to make a linked list, with a separate type for the end of the list:

@sum_type LinkedList begin
    BranchNode(val::Float64, child::LinkedList)
    EndNode(val::Float64)
end

However, this LinkedList type referenced in BranchNode is actually generic and will incur type inference issues, as the sum type is parametrized:

julia> typeof(BranchNode(0.1, EndNode(0.1)))
LinkedList{8, 1, UInt8}

Is there a way I can get this same parametrization within the @sum_type macro, so that I may use it to build recursive SumTypes?

Cheers,
Miles

@MilesCranmer
Copy link
Author

One solution would be to have the macro find all instances of the same sum type within the scope, and add additional parameters to them. So, for example,

@sum_type LinkedList{T} begin
    BranchNode(val::T, child::LinkedList{T})
    EndNode(val::T)
end

would have the referenced type expanded to

BranchNode(val::T, child::LinkedList{T,8,1,UInt8})

@MasonProtter
Copy link
Owner

Ah good point, I didn't think of that. Hmm, I'll need to think about the best way to approach this, it's actually a little tricky.

@MasonProtter MasonProtter changed the title Recursive SumType? Dealing with extra parameters in recursive sumtypes Jun 9, 2023
@MilesCranmer
Copy link
Author

Anything I can help with on this?

@MasonProtter
Copy link
Owner

Hey @MilesCranmer I don't think so. I've made a big overhaul going back to unions, but being smarter about some thing while handling them in #49, so there won't be any extra type parameters after that merges.

@MilesCranmer
Copy link
Author

Nice!!

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

Successfully merging a pull request may close this issue.

2 participants