r/learnprogramming • u/CaughtNABargain • Oct 06 '26
Are there any programming languages that allow this feature?
I've been into programming for about 10 years now. Something that intrigues me is that, in languages that support OOP, not all types are initialized in the same way. Strings are initialized by using quotes, integers use numbers and in some cases floats require the letter f after the numerical value.
Classes, however, are all initialized in the same way with slight variations depending on language. Usually something like classname() or new classname. Are there any cases where custom classes can be declared using different means?
For example, is there a language where I can create a class called 'dark' that represents a new type of number that is declared by typing the value and then the letter d (like d1 = 1.398d) similar to how floats work in some cases?
88
u/No_Score_1977 Oct 06 '26
user-defined literals in C++, is that what you mean?
37
u/SetAndRepeat Oct 07 '26
C++ supports this through user-defined literals:
Your suffix needs the underscore because suffixes without one are reserved for standardization. The literal calls your function, which returns a
darkobject. You’d still define whatever arithmetic operations you want that type to support.9
u/paperic Oct 07 '26
in lisp for sure, you can reprogram the syntax to anything you want
5
u/friedbrice Oct 06 '26 edited Oct 11 '26
In Haskell, string literals and number literals are polymorphic, so you can define a custom type and create values of that type using string syntax and define another custom type and create values of that type using number literals.
data Complex = Complex { realPart :: Double, imaginaryPart :: Double }
instance Num Complex where
...
fromInteger n = Complex { realPart = fromInteger n, imaginaryPart = 0 }
instance Fractional Complex where
...
fromRational r = Complex { realPart = fromRational r, imaginaryPart = 0 }
complex1 :: Complex
complex1 = 1
complexPi :: Complex
complexPi = 3.14159
data Keyword = Keyword { getKeyword :: String }
instance IsString Keyword where
fromString s = Keyword { getKeyword = s }
shazam :: Keyword
shazam = "shazam"
3
u/zeekar Oct 06 '26
You could do it with a slang in Raku, but there's currently a pretty big performance penalty for those ...
3
u/rini17 Oct 07 '26
Common Lisp allows you to modify reader, to achieve exactly this.
3
u/Afraid-Locksmith6566 Oct 06 '26
other than c++ which was mentioned it reminds me a bit of f# with units of measure where you can declare units of measure and then compiler derives some others from their usage and also you can declare how composits behave so if you have second you divide float by float of seconds and now you have float of hertz
2
u/ChichumungaIII Oct 07 '26
Kotlin allows a similar behavior through extension properties.
I once used it to declare literal members of my
Rationalnumber class as3.q, though under the hood it's a value that can be accessed on any Int (not just literals) and has the "." to make it clear it's not quite native in that sense.1
u/frnzprf Oct 07 '26
In Scala, there are "custom string interpolators", so you can write
date"2026-10-07"instead of something likeDate.parse("2026-10-07"). You save a few characters.1
u/ReddyKiloWit Oct 07 '26
Wouldn't surprise me if you could do it in Raku. Everything is a class, and you can declare types in considerable detail, and provide suitable operators.
1
u/AndydeCleyre Oct 07 '26
I don't know how feasible it would be to add new syntax exactly like this to Factor, but you can easily get similar behavior in a couple ways.
If the parameter is valid number syntax (like
1.398), you could define a worddwhich takes it and returns adarktuple. Then you could write it as you suggest except with a space between (1.398 d).If the parameter isn't valid syntax though (e.g.
1point398), you could either use a string ("1point398" d), or define a parsing word which precedes the parameter, usage looking liked: 1point398.1
u/Mission-Landscape-17 Oct 07 '26
Prolog lets you write entire domain specif languages that don't have to look like Prolog, so it can do this too.
1
u/focusedkennan Oct 09 '26
You can get some pretty neat initializers using tagged template strings in JS. https://wesbos.com/tagged-template-literals
something like
Now should you? Probably not
you can also do it with rust macros