3 ms·
From the article: > I’d like a language that can contextually resolve enums, so I can just type something like calc_formula( ia, ib, is_gain ). Swift does thi
by cormacrelf 5y ago
From the article:
> I’d like a language that can contextually resolve enums, so I can just type something like calc_formula( ia, ib, is_gain ).
Swift does this and it should be considered for any new language design. Enum.variant can be shortened to .variant, including for variants with data in them, .variant(arg). Perfect solution, because the dot is an autocomplete trigger.
- henrikeh 5y agoFwiw, Ada does this too.
- Camillo 5y agoDoes this feature have a general name? Swift users seem to call it "dot syntax", which is not a good name. I want to google "C++ should have <this feature>" and find a proposal from 2013 that never moved forward, but "C++ should have dot syntax" is just silly.
- henrikeh 5y agoAda doesn't use the "dot syntax"; instead the enumeration literal is written as is without a dot. If I write: type Number_System is (Bin, Dec, Oct); type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); procedure Foo (A: Number_System; B: Month); Then this is a valid call: Foo( Dec, Dec ) https://godbolt.org/z/eP5qMj3K1 https://godbolt.org/z/eP5qMj3K1 When the type is explicit, the Ada standard calls this a "Qualified expression". But I would just say that it is a kind of type inference for enumerations. https://www.adaic.com/resources/add_content/standards/05aarm/html/AA-4-7.html https://www.adaic.com/resources/add_content/standards/05aarm...
- cormacrelf 5y agoIt’s not just for enums in Swift, it’s applicable to all kinds of static functions, constants, initialisers. Has to be static (in the Swift/Java/etc sense) to work. Maybe “contextual members” or “contextual statics”. I like the latter.
- mayoff 5y agoThe official Swift name of the syntax is “implicit member expression”. https://docs.swift.org/swift-book/ReferenceManual/Expressions.html#ID394 https://docs.swift.org/swift-book/ReferenceManual/Expression...