Is order of a Ruby hash literal guaranteed?

There are couple of locations where this could be specified, i.e. a couple of things that are considered “The Ruby Language Specification”: the ISO Ruby Language Specification the RubySpec project the YARV testsuite The Ruby Programming Language book by matz and David Flanagan The ISO spec doesn’t say anything about Hash ordering: it was written … Read more

Objective-C literals for NSSet and NSOrderedSet?

There is no Objective-C literal syntax for NSSet. It doesn’t really make sense to use index subscripting to access NSSet elements, although key-based subscripting might. Either by wrapping or subclassing, you could add your own. (Correction): Originally I had thought NSOrderedSet wasn’t supported either, but it turns out that using index subscripting is supported, but … Read more

Why doesn’t C have binary literals?

According to Rationale for International Standard – Programming Languages C ยง6.4.4.1 Integer constants A proposal to add binary constants was rejected due to lack of precedent and insufficient utility. It’s not in standard C, but GCC supports it as an extension, prefixed by 0b or 0B: i = 0b101010; See here for detail.

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

You can do that with literal types. from typing_extensions import Literal # from typing import Literal # Python 3.8 or higher def fun(b: int) -> Literal[“a”, “b”, “c”]: if b == 0: return “a” if b == 1: return “b” return “d” mypy is able to detect the return “d” as a invalid statement: error: … Read more

String literal with triple quotes in function definitions

What you’re talking about (I think) are called docstrings (Thanks Boud for the link). def foo(): “””This function does absolutely nothing””” Now, if you type help(foo) from the interpreter, you’ll get to see the string that I put in the function. You can also access that string by foo.__doc__ Of course, string literals are just … Read more

Deprecated conversion from string literal to ‘char*’ [duplicate]

The strings that you enter: “red”, “organge” etc are “literal”, because they are defined inside the program code itself (they are not read directly from disk, user input /stdin etc.). This means that if at any point you try to write to your colors you will be directly accessing your original input and thus editing … Read more