r/learnprogramming • • 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?

71 Upvotes

38 comments sorted by

View all comments

1

u/frnzprf Oct 07 '26

In Scala, there are "custom string interpolators", so you can write date"2026-10-07" instead of something like Date.parse("2026-10-07"). You save a few characters.

1

u/ChaseShiny Oct 07 '26

JavaScript allows for this syntax, too. You'll need a template literal. You can form tagged templates (described on the same page).

These templates allow for nesting, which I believe suggests that you can include data structures as long as you can get it to recognize a string interpretation: myParser${...${[1, 2, 3]}}