taylor swift taylor swift - 19 days ago 5
Swift Question

Import a single constant

The Swift documentation says I can import a value using

import [kind]
, and that I have the options
typealias
struct
class
enum
protocol­
,
var
, and­
func
. So I can import e for example, like this:

import var Glibc.M_E


But this imports
M_E
as a
var
variable. How do I import
M_E
as a
let
constant?

Answer

Like other "simple" C macros, the definition

#define M_E         2.71828182845904523536028747135266250   /* e              */

from <math.h> is imported to Swift as a read-only global property:

public var M_E: Double { get } /* e              */

It is a var, but you cannot change its value:

print(M_E) // OK
M_E = 2.0 // error: cannot assign to value: 'M_E' is a get-only property