Questions tagged [literals]

a notation for representing fixed values in source code

External links

1554 questions
62
votes
6 answers

String literal with triple quotes in function definitions

I am following the Python tutorial and at some point they talk about how the 1st statement of a function can be a String Literal. As far as the example goes, this String Literal seems to be done with three "s, giving in the example """Print a…
Estarius
  • 1,219
  • 3
  • 13
  • 24
60
votes
3 answers

Deprecated conversion from string literal to 'char*'

I have a program which declares an array of strings like this: char *colors[4] = {"red", "orange", "yellow", "blue"}; But I get the above compiler warning. It compiles but I'd rather use the non-deprecated way(if there is one). I've tried to find…
Matt
  • 715
  • 1
  • 5
  • 6
60
votes
6 answers

How to declare an array inline in VB.NET

I am looking for the VB.NET equivalent of var strings = new string[] {"abc", "def", "ghi"};
erik
  • 6,406
  • 3
  • 36
  • 36
54
votes
4 answers

Literal Syntax For byte[] arrays using Hex notation..?

The compiler seems to be ok with this (single digit hex values only): byte[] rawbytes={0xa, 0x2, 0xf}; But not this: byte[] rawbytes={0xa, 0x2, 0xff}; I get a "Possible Loss of Precision found : int required : byte" error? What am I doing wrong -…
monojohnny
  • 5,894
  • 16
  • 59
  • 83
53
votes
3 answers

Type hint for a function that returns only a specific set of values

I have a function that can only return a, b or c, all of them are of type T. I want to include this fact in the signature because of the special meaning they carry in the context of the function. How do I do that? Currently, I use this def fun(...)…
Copperfield
  • 8,131
  • 3
  • 23
  • 29
51
votes
2 answers

Why can I compare a String to a &str using if, but not when using match?

I'm trying to implement a function that reads command line arguments and compares them to hard-coded string literals. When I do the comparison with an if statement it works like a charm: fn main() { let s = String::from("holla!"); if s ==…
robfuscator
  • 631
  • 1
  • 5
  • 13
51
votes
6 answers

Multicharacter literal in C and C++

I didn't know that C and C++ allow multicharacter literal: not 'c' (of type int in C and char in C++), but 'tralivali' (of type int!) enum { ActionLeft = 'left', ActionRight = 'right', ActionForward = 'forward', ActionBackward =…
topright gamedev
  • 2,617
  • 7
  • 35
  • 53
51
votes
10 answers

Why doesn't C have binary literals?

I am frequently wishing I could do something like this in c: val1 &= 0b00001111; //clear high nibble val2 |= 0b01000000; //set bit 7 val3 &= ~0b00010000; //clear bit 5 Having this syntax seems like an incredibly useful addition to C with no…
Drew
  • 12,578
  • 11
  • 58
  • 98
49
votes
2 answers

Objective-C literals for NSSet and NSOrderedSet?

What, if any, NSSet and NSOrderedSet operations can one perform with the new Objective-C collection literals? For NSArray, NSDictionary, and NSNumber, see here. FWIW, this Big Nerd Ranch Post says the indexing syntax is supposed to work for…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
47
votes
4 answers

How to use digit separators for Python integer literals?

Is there any way to group digits in a Python code to increase code legibility? I've tried ' and _ which are digit separators of some other languages, but no avail. A weird operator which concatenates its left hand side with its right hand side could…
Utkan Gezer
  • 3,009
  • 2
  • 16
  • 29
47
votes
2 answers

Is order of a Ruby hash literal guaranteed?

Ruby, since v1.9, supports a deterministic order when looping through a hash; entries added first will be returned first. Does this apply to literals, i.e. will { a: 1, b: 2 } always yield a before b? I did a quick experiment with Ruby 2.1 (MRI) and…
mahemoff
  • 44,526
  • 36
  • 160
  • 222
46
votes
3 answers

How can I use a dynamic format string with the format! macro?

I want to use the format! macro with a String as first argument, but because the macro expects a string literal, I am not able pass anything different to it. I want to do this to dynamically add strings into the current string for use in a view…
Sune
  • 607
  • 2
  • 8
  • 17
45
votes
3 answers

How does one escape characters in Delphi string

Delphi strings use single quotes, for example 'a valid string'. How does one specify the ' character within a literal string? How would one refer to the null byte (Unicode code point U+0000)?
Boaz
  • 25,331
  • 21
  • 69
  • 77
43
votes
6 answers

in CoffeeScript, how can I use a variable as a key in a hash?

eg: So: foo = "asdf" {foo: "bar"} eval foo # how do I get {"asdf": "bar"} ? # this will throw parse error: {(eval foo): "bar"} This is a simple syntax question: how do I get CoffeeScript to construct a hash dynamically, rather than doing it by…
Giles Bowkett
  • 529
  • 1
  • 4
  • 6
43
votes
3 answers

Why is this string key in a hash converted to a symbol?

Using Ruby 2.3: In example 1, the string key "a" is automatically converted to a symbol, whereas with example 2, it stays a string. Example 1 {"a": 1} # => {:a=>1} Example 2 {"a"=>"c"} # => {"a"=>"c"} I thought : was the same as the old style…
Nona
  • 5,302
  • 7
  • 41
  • 79