Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers? - scala

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers:
`0029-otherwise-illegal-scala-literal`
See Scala explanation in Need clarification on Scala literal identifiers (backticks)

You can find the spec at https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#8.2
Section 2.2.2 tells you
The PropertyName production from the ECMAScript grammar is reproduced
below:
  PropertyName:    LiteralPropertyName    ComputedPropertyName
  LiteralPropertyName:    IdentifierName    StringLiteral
   NumericLiteral
  ComputedPropertyName:    [ AssignmentExpression ]
A property name can be any identifier (including a reserved word), a
string literal, a numeric literal, or a computed property name. String
literals may be used to give properties names that are not valid
identifiers, such as names containing blanks. Numeric literal property
names are equivalent to string literal property names with the string
representation of the numeric literal, as defined in the ECMAScript
specification.
This includes string literals.
You can declare a property as a string literal:
class MyClass {
"return" = 1;
}
you can access it with square brackets
let myinstance = new MyClass()
let one = myinstance["return"]

Related

Swift: Simple method to replace a single character in a String?

I wanted to replace the first character of a String and got it to work like this:
s.replaceSubrange(Range(NSMakeRange(0,1),in:s)!, with:".")
I wonder if there is a simpler method to achieve the same result?
[edit]
Get nth character of a string in Swift programming language doesn't provide a mutable substring. And it requires writing a String extension, which isn't really helping when trying to shorten code.
To replace the first character, you can do use String concatenation with dropFirst():
var s = "😃hello world!"
s = "." + s.dropFirst()
print(s)
Result:
.hello world!
Note: This will not crash if the String is empty; it will just create a String with the replacement character.
Strings work very differently in Swift than many other languages. In Swift, a character is not a single byte but instead a single visual element. This is very important when working with multibyte characters like emoji (see: Why are emoji characters like 👩‍👩‍👧‍👦 treated so strangely in Swift strings?)
If you really do want to set a single random byte of your string to an arbitrary value as you expanded on in the comments of your question, you'll need to drop out of the string abstraction and work with your data as a buffer. This is sort of gross in Swift thanks to various safety features but it's doable:
var input = "Hello, world!"
//access the byte buffer
var utf8Buffer = input.utf8CString
//replace the first byte with whatever random data we want
utf8Buffer[0] = 46 //ascii encoding of '.'
//now convert back to a Swift string
var output:String! = nil //buffer for holding our new target
utf8Buffer.withUnsafeBufferPointer { (ptr) in
//Load the byte buffer into a Swift string
output = String.init(cString: ptr.baseAddress!)
}
print(output!) //.ello, world!

Argument `#1' cannot convert `string' expression to type `char[]'

The best overloaded method match for `string.Split(params char[])' has some invalid arguments
Argument `#1' cannot convert `string' expression to type `char[]'
I'm trying to make a Text Dialogue but this error prevents me from compiling. What's wrong?
public TextAsset textFile;
public string[] textLines;
// Use this for initialization
void Start() {
if (textFile != null)
{
textLines = (textFile.text.Split("\n"));
}
}
string.Split has a couple of different overloads (combinations of parameters it can take), but none of them take a single string parameter. "\n" is a string literal, so it's an invalid argument.
One of the overloads takes a params char[], meaning you can either pass an array of chars, or you can just pass a bunch of individual chars and it will make the array for you. So you can use test.Split('\n') because single quotes ' denote a char literal rather than a string literal.

Swift variable name with ` (backtick)

I was browsing Alamofire sources and found a variable name that is backtick escaped in this source file
open static let `default`: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
return SessionManager(configuration: configuration)
}()
However in places where variable is used there are no backticks. What's the purpose of backticks?
Removing the backticks results in the error:
Keyword 'default' cannot be used as an identifier here
According to the Swift documentation :
To use a reserved word as an identifier, put a backtick (`)before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning.
In your example, default is a Swift reserved keyword, that's why backticks are needed.
Example addendum to the accepted answer, regarding using reserved word identifiers, after they have been correctly declared using backticks.
The backticks are not considered part of the identifier; `x` and x
have the same meaning.
Meaning we needn't worry about using the backticks after identifier declaration (however we may):
enum Foo {
case `var`
case `let`
case `class`
case `try`
}
/* "The backticks are not considered part of the identifier;
`x` and x have the same meaning" */
let foo = Foo.var
let bar = [Foo.let, .`class`, .try]
print(bar) // [Foo.let, Foo.class, Foo.try]
Simply put, by using backticks you are allowed to use
reserved words for variable names etc.
var var = "This will generate an error"
var `var` = "This will not!"

PowerShell class array of strings syntax

I have the following class, but instead of a [String] member variable I need to declare an array of Strings. What's the syntax?
class SomeClass{
[String] $patterns;
SomeClass(){
$this.patterns = "Maintenance Code","Page \d+ of";
}
[Bool]SomeMethod([string]$txt){}
}
So as PetSerAl said [System.String[]] but [String[]] is just fine.
It's also mentioned in help about_arrays.
To create a strongly typed array, that is, an array that can contain only
values of a particular type, cast the variable as an array type, such
as string[], long[], or int32[]. To cast an array, precede the variable
name with an array type enclosed in brackets. For example, to create a
32-bit integer array named $ia containing four integers (1500, 2230, 3350,
and 4000), type:
[int32[]]$ia = 1500,2230,3350,4000

Double underscore prefix in Swift variables

In the Advanced Swift talk, the generated code for an for loop is described as:
var __g: Generator = mySequence.generate()
while let x = __g.next() {
// iterations here
}
Does the underscore prefix have some specific meaning?
No, the underscore- and double-underscore-prefixes have no specific meaning in Swift. They conventionally imply "private" or "internal" declarations, but you can define variables beginning with as many underscores as you like.