Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- C
- Clojure
- C++
- C#
- D
- D
- D
- D
- D
- Dart
- Elixir
- Erlang
- Fortran
- Fortran
- Go
- Groovy
- Haskell
- JS
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- PHP
- Pascal
- Perl
- Perl
- Python
- Ruby
- Rust
- Scala
- Smalltalk
- VB
planet : constant String := "Earth";
const char *_planet = "Earth";
(def planet "Earth")
Clojure vars are constants by default.
const string _planet = "Earth";
enum string planet = "Earth";
This is a compile-time constant.
static immutable string planet = "Earth";
This is a compile-time constant.
enum planet = "Earth";
This is a compile-time constant.
immutable string planet = "Earth";
This is a runtime constant.
const string planet = "Earth";
This is a runtime constant.
const String _planet = "Earth";
planet = "Earth"
Elixir variables are immutable. You can rebind the name planet but this creates a new variable with the same name.
Planet = "Earth".
All variables are unmutable.
character(*), parameter :: planet = "Earth"
(sorry this is redundant with an already existing snippet; please delete this one)
character(len=*), parameter :: planet = "Earth"
static final planet = "Earth"
static final planet = 'Earth'
static final planet = /Earth/
static final planet = '''Earth'''
static final planet = """Earth"""
Various ways to define strings in Groovy.
planet = "Earth"
In Haskell, definitions like this one are always immutable.
const planet = 'Earth'
It's considered good practice to use const unless your variable is mutable
final String planet = "Earth";
final String planet = "Earth";
Use this when it is a local variable; when contained within a method.
(defparameter +planet+ "Earth")
This is not technically constant, but is the most common way of doing it. The third-party library Alexandria provides define-constant that could define a constant and one could also use define-symbol-macro from the standard library
PLANET = "Earth"
Not a constant but closest implementation
NSString * const Planet=@"Earth";
There's a (weak) convention to keep constants capitalized
const
planet = 'Earth';
const my $planet => 'Earth';
Module Const::Fast is preferred over Readonly, which is greatly preferred over 'use constant'. Google "Perl Critic Policy Prohibit Constant Pragma" and read the Const::Fast documentation for more details.
use constant planet => 'Earth';
use constant PI => 3.14159;
use constant { RED => 1, BLUE => 2, GREEN => 3 };
Perl's classic way to create constants. For various reasons, module Readonly (or even better, Const::Fast) are now preferred.
PLANET = 'Earth'
Names of constants are by convention written in uppercase
Planet = 'Earth'
Constants start with a capital.
val planet = "Earth"
planet := 'Earth'.
Literal strings in Smalltalk are immutable
Const PLANET = "Earth"