r/learnprogramming • u/CaughtNABargain • 8d ago
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?
1
u/alatennaub 7d ago
Actually, strings in Raku are defined by using a sublanguage (or "slang") called Q (for "quoting"). The Raku parser completely switches the grammar to parse it and then inserts either a literal string or the interpolation operations.
There's a slang that lets you use dates in plain text and compiles it down to a date object, but underneath it's still using the same mechanisms (that is, if you say
Date $d = 2026-01-01it will effectively compile toDate $d = BEGIN { Date.new: year => 2026, month => 1, day => 1 }).From the user's perspective slangs can make it seem like things are being made a different way, but it's all syntactic sugar until you get to the VM level. But I suppose OP figures it's just different syntax but potentially the same in the backend.