4 ms·
> So, rather than writing > > @Test("Strip HTML tags from string") func stripHTMLTagsFromString() {...} > > we can instead write > > @Test func `Strip HTML tags
by 90s_dev 1y ago
> So, rather than writing
>
> @Test("Strip HTML tags from string") func stripHTMLTagsFromString() {...}
>
> we can instead write
>
> @Test func `Strip HTML tags from string`() {...}
Maybe I'm just really new at programming, but this seems like an absolutely bad feature, and the example actually perfectly proves it:
You really want to name a function "Hello World!" instead of helloWorld, just so your stack traces can pass a 5th grade English class exam?
- seanmcdirmid 1y agoTests are never called explicitly by programmers, or at least they never should be. You could argue that they don’t really need to be functions at all, just pieces of code that represent tests.
- SwiftyBug 1y agoThat's how Zig tests work. This is basically a function with a different syntax. test "strip html tags from string" { ... }
- 90s_dev 1y agoIt just seems to me that this is the exactly wrong way to solve a programming problem. If the problem boils down to "I want some variables (almost always test function names) to be more human readable", the solution should never be "hey let's add this feature to the core language and make identifiers use any ASCII string! I dunno, maybe I'm wrong here and being overly critical. But to me it just screams "Swift has lost the plot."
- bbatsell 1y agoThe HTTPStatus enum example is a good one, but the backtick syntax is _rough_. I would only ever use the Type.case form in practice. The test stuff is basically a way to create BDD-style test names, which is kind of just a preference thing. I can’t envision myself using it for anything other than weird case names (I already use case `default` quite a lot because it’s such a useful word), but maybe some interesting DSLs can come out of it? I would not have prioritized that change personally.
- 90s_dev 1y agoI would not have even approved it. But that's just me.
- tiltowait 1y agoThe backtick syntax for enums is rough, and typing out the full Type.case form negates one of Swift’s niceties (the ability to just type .case if the compiler can tell which enum is in use).
- rTX5CMRXIfFG 1y agoThe HTTP status enum example is a _terrible_ one. If you need to represent HTTP statuses and call them by their integer names, why not just pass the damn integer itself? It's exactly what you get in `HTTPURLResponse.statusCode`, and you can already `switch` against it. Already not looking forward to the code that undiscerning devs will mindlessly write just because someone with a huge following wrote a blog.
- dagmx 1y agoYou can’t exhaustively handle an enum with just an integer.
- layer8 1y agoYou want to exhaustively handle all 500 valid HTTP status codes (cf. RFC 9110)?
- dagmx 1y agoExhaustive matching doesn’t necessarily mean you handle every case separately, but it means you aren’t going to get cases you don’t know about sneaking in. With a bare int you can get values outside a range and the compiler won’t help
- layer8 1y agoWhen you receive an HTTP response, it can contain a status value outside the valid range as well. So you have to handle those one way or the other. Regarding the int type, a better solution would be to provide the ability to define a restricted integer type, so that the compiler can help.
- nulld3v 1y agoMany languages have had this feature for a long time. Ruby, ~~Rust,~~ Kotlin, etc... It hasn't been an issue at all. I like that most languages seem to have reached consensus on backticks or other similarily awkward characters for this feature. Typing these identifiers is like hitting a speed bump, which seems to be enough to make devs avoid them except for very specific use-cases.
- 0rzech 1y agoRust does not have this feature. Function names can't have spaces, for example. Underscores are used instead. Maybe there are some macros for arbitrary ASCII strings - I don't know - but it's not a language feature.
- nulld3v 1y agoWow, you are correct! A Rust "raw identifiers" can be a reserved keyword. But other than that, all other normal identifier naming rules still apply... And apparently I never figured this out even after 3 years of Rust lol, thanks!
- nulld3v 1y agoWhoops, I had an edit that apparantly did not submit. My comment is very wrong now, the only language in that list that supports "raw identifiers" in the same sense as Swift is Kotlin. Although I still stress that it has never been an issue in Kotlin.
- jibal 1y agoZig uses @"arbitrary name" It's an important feature for FFI, as well as passing operator functions around. (It seems bizarre to me that you can't do `+` in Swift, but I don't know Swift so maybe there's another way to name such functions.) Also, the Zig library now uses @"..." for variables with the same name as keywords, rather than using ad hoc naming conventions in those cases.
- raydev 1y ago> languages have had this feature for a long time. Ruby Yes, that is precisely why I don't like Ruby, it's actually impossible for tools to reason about many things that would make finding bugs before shipping feasible. Big companies like Shopify have to impose a lot of restrictions on it to make it work at scale, which is silly. Just use a different language! Now Swift may not be in this situation because it's added yet more characters to wrap this nonsense so it is possible to reason about, but it's still just unnecessary, and I will be adding a lint rule against it at work. I don't expect a lot of pushback if any.
- deleted 1y ago[deleted]
- jibal 1y agoTest names are just an example. There are other valid reasons to be able to use arbitrary strings as names--importing functions from other languages and names that clash with keywords, for example.
- hiccuphippo 1y agoNaming things is hard. If you can more accurately describe your test and not think of a separate name for it I'm all for it. Also I like the backticks better than what zig came up with: @"404"
- thedanbob 1y agoI don't know about other languages but Ruby is similar in that you can name a function with any string (though you might not be able to call it in the standard way) and the Rails default test framework takes advantage of that.
- Jtsummers 1y agohttps://tio.run/##S87JLC74/18jJTWtNE@hJiM1JydfoTy/KCelRkFDk0tBQSMtvyg3sUShREHJAyGpWKeqpKnJpYGiQfP/fwA https://tio.run/##S87JLC74/18jJTWtNE@hJiM1JydfoTy/KCelRkFDk0... Common Lisp allows it as well, though I don't think I've ever seen it done outside a demonstration that it can be done.
- soegaard 1y agoThe feature is fine but there are better reasons to introduce it. For macro generated code it is convenient to use identifiers that people won't accidently use in their own code. The ability to use arbitrary identifiers solve that.
- codr7 1y agoExcept people can also use arbitrary identifiers in their own code now?
- int_19h 1y agoThe other reason is interop with other languages, which might have their own rules for identifiers.
- codr7 1y agoI'm trying and failing miserably to think of a single example of such a language.
- akdor1154 1y ago.NET allows pretty much anything except a space, i think?
- codr7 1y agoAnd you can call into .NET (I assume that means C#) directly from Swift?
- neonsunset 1y agoKind of? :) https://github.com/royalapplications/beyondnet https://github.com/royalapplications/beyondnet
- layer8 1y agoYou can always work with aliases in that case. No strict need to “contaminate” a language with foreign identifier syntax, in particular if it’s still not 1:1 due to the backticks (e.g. identifiers containing backticks).
- deleted 1y ago[deleted]
- Starlevel004 1y agoKotlin has this, but it's basically only ever used for tests. I've never seen a real world method with a backtick name.
- rcruzeiro 1y agoThis is useful for testing. Currently, I need to write both the string name of the text and an identifier based on that name for the function itself. Soon I will only need to write it once. This is not much useful for much else though, and just because you can write code like this, it does not mean you should.
- 90s_dev 1y agoA better solution is to have a testing framework that doesn't rely on functions to name tests, especially in a language that has anonymous functions.
- rcruzeiro 1y agoWe have Quick for this — and while this framework is still a reasonable choice of a testing framework, I personally feel like the new Swift Testing framework is much nicer to write my test cases with. https://github.com/Quick/Quick https://github.com/Quick/Quick
- wingerlang 1y agoWhy do you need both an identifier and a text? I have this test name 'testAddingNewCardDataResultsInProperlyCombinedCardDataButNoNewCardsUnlocksBecauseWeStillHaveUnlearnedCards', and even though it is much longer than my other ones, it is still perfectly readable, and even if it wasn't, the only time I actually have to read it, is if it fails.