4

on Swift String is a struct and you could just initialize it using

var someString:String = "Hello"

how would I make another Struct initializable like String?

for example

struct StringV2 {
    init()
}

class SomeClass {
    let someStringV2:StringV2 = "Hello"
}

Since that's how String's code looks like.

4
  • I think this might help you stackoverflow.com/questions/25112081/… Commented Nov 2, 2016 at 6:40
  • For Swift 2 you'll find an example here: stackoverflow.com/a/27713005/1187415. Commented Nov 2, 2016 at 6:54
  • 1
    We don't know what your intentions are, but writing up a new string type is a pretty niche thing that pretty much never comes up. Most likely what you're trying to achieve can be more easily, and better, done using an extension. Commented Nov 2, 2016 at 7:09
  • @AlexanderMomchliov yep I know of extensions but right now I'm just trying to make a new string type. for learning purposes :) Commented Nov 2, 2016 at 7:49

1 Answer 1

7

This is (in my opinion) neat part of the language. Yes, this is possible, thanks to the ExpressibleByStringLiteral protocol.

Unfortunately, there is some complexity to it. ExpressibleByStringLiteral inherits from ExpressibleByExtendedGraphemeClusterLiteral, which itself inherits from ExpressibleByUnicodeScalarLiteral. Thus, to conform to the first, you must conform to the other 2 above it.

This makes it possible for your struct or class to be initialized from:

  • A UnicodeScalarLiteralType (such as a UnicodeScalar, which is a single Unicode code point, e.g. "A")
  • An ExtendedGraphemeClusterLiteralType (such as a Character, which is a collection of UnicodeScalars, such as "🐥")
  • A StringLiteralType (such as String, which is a collection of Characters, such as "This is a string")

Here's an example implementation that just sets a String member variable:

struct StringV2: ExpressibleByStringLiteral {
    let s: String

    init(unicodeScalarLiteral: UnicodeScalar) {
        s = String(unicodeScalarLiteral)
    }

    init(extendedGraphemeClusterLiteral: Character) {
        s = String(extendedGraphemeClusterLiteral)
    }

    init(stringLiteral: String) {
        s = stringLiteral
    }
}

let s1: StringV2 = "This is a string" // String
print(s1.s)

let s2: StringV2 = "A" // Unicode scalar
print(s2.s)

let s3: StringV2 = "🐥" // Extended grapheme cluster
print(s3.s)
Sign up to request clarification or add additional context in comments.

11 Comments

I guess whether you give "A" or "🐥", it will go to init(stringLiteral: StringLiteralType) only
You can also omit all the type aliases and define init(stringLiteral: String) etc.
@MartinR Oh yeah, it'll just infer the associated types. Nifty.
@RajanMaheshwari Interesting observation. And casting doesn't make it work either. What's up with that?
Probably thats a Character and you have to make that with conforming the ExpressibleByExtendedGraphemeClusterLiteral
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.