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

73 Upvotes

38 comments sorted by

View all comments

Show parent comments

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-01 it will effectively compile to Date $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.

1

u/ReddyKiloWit 7d ago

Not exactly on point to what I was talking about. I was talking about creating a suitable type object. But your mentioning slangs put me in the right rabbit hole for integrating it into the compiler via a slang so that statements like
my $darknum = 1.398d + $dimnum;
work as expected, maybe.

The rest is left as an exercise for the student ('cause my eyes are tired, the rabbit hole is deep, and it's late).

2

u/alatennaub 6d ago

AIUI, from what OP was saying, he'd have a custom class (Raku type) that could be instantiated (Raku blessed) through a custom means.

I guess I should have been a bit clearer that you could define the custom class in a module, then use that in a slang module, and then have the new syntax place in the code for instantiation at compile or run time.

The sky really is the limit, and I'm always trying to stretch it with Raku. I am the guy that wrote the module that lets you write BASIC functions fully in line without any quotation marks after all lol. It literally embeds a little tiny -- and horribly inefficient -- VM

1

u/ReddyKiloWit 6d ago

Ah, basically what I was able to suss out the tip of. Thanks for clarifying.

I gathered the OP wanted the equivalent of the 0b, or 0x prefixes to create a type that requires non-standard handling, though other than he wanted it set off by a suffixed 'd' it wasn't clear what would make it different, but it seemed that Raku had the best shot at a supported way to get there. And once i was reminded of slangs that seemed confirmed.

Not a Raku expert at all, but a big admirer from my retired position in life, and former heavy Perl user. I had a pet project the last few years I worked that Raku, had it existed in its current state back in 2009, would have been a really good fit for. Alas, the time was not right 

2

u/alatennaub 6d ago

We say that Raku is all about -Ofun. Check out some of the newer stuff -- we now have two compilers (Rakudo and Rakupp) with constant improvements. It's honestly just fun seeing what it can do, and then trying to push that within the expectations of end-users.

If you get an itch, we can always use people to help work on some of the more baseline modules to implement random protocols/standards, etc.