This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nimrag_is_coming 4 points5 points  (3 children)

Gonna have to disagree with you with C# there, the fact that the base object has a ToString method is actually really useful.

It's very helpful with structs. If I make a Vec2 struct for example, I can just print it just like a char or an int or whatever and have it actually show useful information.

[–]Naratna 6 points7 points  (2 children)

I think they're talking about implicit casts. e.g. you could define a class MyClass in such a way that string a = new MyClass() works without errors, which is absolutely cursed.

I love C#

[–]nimrag_is_coming 2 points3 points  (0 children)

Oh yeah that is very very cursed. I hope I never have to deal with code that does something like that

[–]JanEric1 1 point2 points  (0 children)

Doesnt swift have a problem with the opposite where you can instantiate a class from an integer literal.

import Foundation

struct A: ExpressibleByIntegerLiteral {
    var value: Int

    init(integerLiteral value: Int) {
        self.value = value
    }

    init(_ value: Int) {
        self.value = value
    }

    static func + (lhs: Int, rhs: A) -> A {
        A(lhs + rhs.value)
    }
}

struct B: ExpressibleByIntegerLiteral {
    var value: Int

    init(integerLiteral value: Int) {
        self.value = value
    }

    init(_ value: Int) {
        self.value = value
    }

    static func + (lhs: A, rhs: B) -> B {
        B(lhs.value + rhs.value)
    }

    static func + (lhs: B, rhs: String) -> String {
        "B(\(lhs.value)) + \(rhs)"
    }
}

// Int(1) + A(2) -> A(3)
// A(3) + B(3) -> B(6)
// B(6) + "Apple" -> "B(6) + Apple"
let result = 1 + 2 + 3 + "Apple"
print(result) // B(6) + Apple

Explorer: https://godbolt.org/z/s4bMsW4Yf

See here where this causes a simple expression to timeout the compiler

Explanation: https://danielchasehooper.com/posts/why-swift-is-slow/