If you could redesign ExpressibleByExtendedGraphemeClusterLiteral, how would you do it?

disclaimer: the purpose of this thread is not to reinvent ExpressibleByExtendedGraphemeClusterLiteral, the purpose is to gather feedback on what the future relationship between Decimal literals and our existing floating point literals should be.

syntactically, String, Character, and Unicode.Scalar literals all look identical. this would cause typechecker ambiguity, if an Expressible type conformed to more than one of:

  • ExpressibleByUnicodeScalarLiteral
  • ExpressibleByExtendedGraphemeClusterLiteral
  • ExpressibleByStringLiteral

to prevent this, ExpressibleByStringLiteral inherits from ExpressibleByExtendedGraphemeClusterLiteral. it provides a default implementation for init(extendedGraphemeClusterLiteral:), which can be overridden, and which also should never be overridden.

ExpressibleByExtendedGraphemeClusterLiteral in turn inherits from ExpressibleByUnicodeScalarLiteral, and so on.

you can set ExtendedGraphemeClusterLiteralType to String (since every Character can be converted to a String), but this does not affect the static “overflow” checking.

struct S:ExpressibleByExtendedGraphemeClusterLiteral 
    init(extendedGraphemeClusterLiteral _:String)

func main() 
    let _:S = ""
error: cannot convert value of type 'String' to specified type 'S'
    let _:S = ""

ignoring ICU-related aspects, Decimal literals have a similar relationship to floating point literals as Unicode.Scalar literals to Character literals. every Decimal literal is a floating point literal, but not every floating point literal is a Decimal literal.

if we get ExpressibleByDecimalLiteral, should ExpressibleByFloatLiteral inherit from it? or should Decimals be just another _BuiltinFloatLiteralType?

At a glance, this seems similar to asking whether or not protocol Square should inherit from protocol Rectangle — to which I would say "yes, it should inherit". But that's just the first analogy that popped into my head, and I'm not sure how appropriate it is. Also, the answer might depend on how deep in the weeds we want stdlib types to go WRT all the different kinds of numbers.

What's an "ICU-related aspect"? My brain keeps trying to expand that as "Intensive Care Unit", but I'm pretty sure that's wrong :laughing:

unicode-related aspect. it’s not important here, which is why it is being ignored.

1 Like
Terms of Service

Privacy Policy

Cookie Policy