Coder Social home page Coder Social logo

Mutating enum self about skip HOT 3 CLOSED

pnewell avatar pnewell commented on June 22, 2024
Mutating enum self

from skip.

Comments (3)

marcprux avatar marcprux commented on June 22, 2024

I don't get a compilation error, but I do see a test case based on it passing on the Swift side and failing on the Kotlin side:

class MutatingEnumTests: XCTestCase {
    enum Count {
        case one
        case two
        case three

        mutating func reset() {
            self = .one
        }
    }

    func testMutatingEnum() throws {
        var b = Count.three
        XCTAssertEqual(Count.three, b)
        b.reset()
        XCTAssertEqual(Count.one, b)
    }
}

This transpiles into MutatingEnumTests.kt:

internal class MutatingEnumTests: XCTestCase {
    internal enum class Count {
        one,
        two,
        three;

        internal fun reset(): Unit = assignfrom(MutatingEnumTests.Count.one)

        private fun assignfrom(target: MutatingEnumTests.Count) = Unit
    }

    @Test
    internal fun testMutatingEnum() {
        var b = Count.three
        XCTAssertEqual(Count.three, b)
        b.reset()
        XCTAssertEqual(Count.one, b)
    }
}

This is a transpiler bug, so I'll move this issue over to the skip repo. Thanks for reporting it!

from skip.

marcprux avatar marcprux commented on June 22, 2024

For completeness, I'll point out that the equivalent test does pass with structs:

@available(macOS 13, *)
final class MutatingStructTests: XCTestCase {
    struct Count : Equatable {
        private let count: Int

        static let one = Count(count: 1)
        static let two = Count(count: 2)
        static let three = Count(count: 3)

        mutating func reset() {
            self = .one
        }
    }

    func testMutatingStruct() throws {
        var b = Count.three
        XCTAssertEqual(Count.three, b)
        b.reset()
        XCTAssertEqual(Count.one, b)
    }
}

which transpiles to:

internal class MutatingStructTests: XCTestCase {
    internal class Count: MutableStruct {
        private var count: Int

        internal fun reset() {
            willmutate()
            try {
                assignfrom(MutatingStructTests.Count.one)
            } finally {
                didmutate()
            }
        }

        private constructor(count: Int) {
            this.count = count
        }

        override var supdate: ((Any) -> Unit)? = null
        override var smutatingcount = 0
        override fun scopy(): MutableStruct = MutatingStructTests.Count(count)

        private fun assignfrom(target: MutatingStructTests.Count) {
            this.count = target.count
        }

        override fun equals(other: Any?): Boolean {
            if (other !is MutatingStructTests.Count) return false
            return count == other.count
        }

        companion object {

            internal val one = Count(count = 1)
            internal val two = Count(count = 2)
            internal val three = Count(count = 3)
        }
    }

    @Test
    internal fun testMutatingStruct() {
        var b = Count.three.sref()
        XCTAssertEqual(Count.three, b)
        b.reset()
        XCTAssertEqual(Count.one, b)
    }
}

So the issue is specific to enums, not to all value types.

from skip.

aabewhite avatar aabewhite commented on June 22, 2024

We're not going to be able to support this and still use Kotlin enums. I added detection and an error message to that effect to the transpiler.

from skip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.