Recursive Data Structures

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Recursive Data Structures

andrew batutin
Hi
Swift is nice with all the functional goodies included int it.
But one thing really annoying for me. 
We can't define recursive data structures

So such code

public struct BinaryTree<T:Comparable>{


    var leftNode:BinaryTree? = nil


    var rightNode:BinaryTree? = nil


    var nodeValue:T? = nil

}

is not compilable cause of 

BinaryTree.swift:9:15: Recursive value type 'BinaryTree<T>' is not allowed
error

I've seen a way around this issue boxing the leaf value at new class. But it looks ugly.
Is there any better way?
Or can we make Apple improve compiler?


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/03fa9f58-7d39-4033-9f54-86a633756832%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Recursive Data Structures

andrew batutin
Hi Andrew
You can just use class instead of struct
It's not perfect. But tolerable.

On Thursday, December 4, 2014 11:36:33 PM UTC+2, andrew batutin wrote:
Hi
Swift is nice with all the functional goodies included int it.
But one thing really annoying for me. 
We can't define recursive data structures

So such code

public struct BinaryTree<T:Comparable>{


    var leftNode:BinaryTree? = nil


    var rightNode:BinaryTree? = nil


    var nodeValue:T? = nil

}

is not compilable cause of 

BinaryTree.swift:9:15: Recursive value type 'BinaryTree<T>' is not allowed
error

I've seen a way around this issue boxing the leaf value at new class. But it looks ugly.
Is there any better way?
Or can we make Apple improve compiler?


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/2f6a1235-550e-4303-8cd9-0163e1372125%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Recursive Data Structures

andrew batutin
In reply to this post by andrew batutin
And how about Empty type?
Is there smthng is Swift to represent Empty type?
Like empty leaf?

On Thursday, December 4, 2014 11:36:33 PM UTC+2, andrew batutin wrote:
Hi
Swift is nice with all the functional goodies included int it.
But one thing really annoying for me. 
We can't define recursive data structures

So such code

public struct BinaryTree<T:Comparable>{


    var leftNode:BinaryTree? = nil


    var rightNode:BinaryTree? = nil


    var nodeValue:T? = nil

}

is not compilable cause of 

BinaryTree.swift:9:15: Recursive value type 'BinaryTree<T>' is not allowed
error

I've seen a way around this issue boxing the leaf value at new class. But it looks ugly.
Is there any better way?
Or can we make Apple improve compiler?


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/38a70d7a-9af6-4f22-ab66-fe73f901b041%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Recursive Data Structures

Jens Alfke

On Dec 4, 2014, at 1:59 PM, andrew batutin <[hidden email]> wrote:

And how about Empty type?
Is there smthng is Swift to represent Empty type?

That's what the "?" syntax is for. It denotes a reference that may be empty.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/EFF7DD34-510F-4BB2-8111-1053A2709675%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Recursive Data Structures

Jeremy Pereira
In reply to this post by andrew batutin
As I understand it, structs are value types.  Trying to define a recursive struct, even using optionals is effectively defining an infinitely large object.

You wouldn't expect this to work in C would you:

struct BinaryTree
{
    struct BinaryTree left;
    struct BinaryTree right;
};

You need to use references, which means in Swift, you need to use a class not a struct.

class Foo<T: Comparable>
{
    var left: Foo<T>?
    var right: Foo<T>?
    var value: T?

    init(left: Foo<T>?, right: Foo<T>?, value: T?)
    {
        self.left = left
        self.right = right
        self.value = value
    }
}

> On 4 Dec 2014, at 21:36, andrew batutin <[hidden email]> wrote:
>
> Hi
> Swift is nice with all the functional goodies included int it.
> But one thing really annoying for me.
> We can't define recursive data structures
>
> So such code
>
> public struct BinaryTree<T:Comparable>{
>
>
>     var leftNode:BinaryTree? = nil
>
>
>     var rightNode:BinaryTree? = nil
>
>
>     var nodeValue:T? = nil
>
> }
>
> is not compilable cause of
>
> BinaryTree.swift:9:15: Recursive value type 'BinaryTree<T>' is not allowed
> error
>
> I've seen a way around this issue boxing the leaf value at new class. But it looks ugly.
> Is there any better way?
> Or can we make Apple improve compiler?
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Swift Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/03fa9f58-7d39-4033-9f54-86a633756832%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/860619AE-9403-428E-ACDC-FA248D072224%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.