There's no language for this

1
2
3
4
5
6
MyFunc(implicit Type typ, typ x, typ y) -> typ
    SomeGlobalVar := typ
    return x + y

MyOtherFunc() -> SomeGlobalVar
    return SomeGlobalVar(7)
This isn't a good use-case example, but it shows what I'm talking about.

EDIT: No idea what I was talking about when I originally wrote this. Please disregard.
Last edited on
it shows what I'm talking about.
I have no idea what you're talking about.
Last edited on
Strongly typed language with types themselves being first-class citizens.
Firstly, that is a very bad example. Secondly, after doing about 10 min research, I have found nothing. What would the purpose be?

EDIT: Assuming I understand your point.
Last edited on
Ah, so I always have to have a practical use-case for these kinds of questions. That's kind of difficult when I don't have solid ground to stand on here. And yes, that is a horrible example - I'll do better when I have some time to set something up.
@LB, java? I'm pretty sure your example can be done in it. Maybe it's arguable what "first class citizen" is.

Can you come up with an example that's more sensible? This one kind of shows what you want, but not at all why you want it.
Neither classes nor primitives are first class citizens in Java, so no. The example definitely can't be done in Java without using the reflection API (which is just an API and not part of the language itself).

As for what first class citizens are, I thought this was a widely used term in programming? E.g. in Python variables, functions, and types are all first class citizens and can be treated the same as all other variables (which is what I want, but Python is weakly typed).
Last edited on
Why didn't you just say "Is there a strongly-typed language that treats types as first-class citizens [i.e. mutable, passable, runtime-constructable objects]?" To be honest, I can't see how it would be useful.
If reflection API is not part of the language, I'm sure you could implement it using the language, no? Likewise, in C++, typeid returns std:type_info class - is that library or language? I too thought as if there was (and should be) some hard distinction between the language and the library, until I tried Scala, I guess.

Apparently there is a definition (I wasn't aware of):
first-class citizen is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable.
I guess java is missing the run time construction?

But how in the world that would any of this work with a statically typed language? I'm here assuming that you wanted a static, rather than strong typing, because why could you possibly care about strong vs weak typing? Of course, it is trivial to show that static type checking with first-class classes is impossible.

And you still haven't said why you could want this...
Last edited on
In C# every type is an object which derives from the object class. Is that in the direction your thinking of or am I way off?
@Oria way off.

@everyone else: you can't see the advantage of being able to store types in a strongly typed language? (And no, I don't mean statically typed, that would be pretty near impossible.) I don't know how I can make the advantages any more clear. Think registering classes at runtime for deserializing a saved game, plugins considered.
Strongly typed language is one that does not have many implicit conversions. Python could be made strongly typed and all the difference would be the difference between "abc" + 5 and "abc" + 5.tostring(). Strong typing is a thing with no impact on the capabilities of a language. Why would you care about it?
Recall that C++ is not strongly typed (nor is it weakly typed, I guess).

Think registering classes at runtime for deserializing a saved game, plugins considered.
What of this is not doable in Java?
(I honestly don't know, I haven't used Java reflection much).
Class<> type in Java does exactly what you ask for. It is a runtime representation of types.


first-class citizen is an entity that can be constructed at run-time


check, see ASM or CGLIB as great examples of how to creates types at runtime

passed as a parameter


check


returned from a subroutine


check


or assigned into a variable.


check

But there is even more:
- it can be used to instantiate a new object of that type
- it can be used for runtime type-checking, including checking inheritance relationships

Last edited on
@hamsterman I was not aware that being strongly typed had anything to do with operator overloading - are you sure of this? I could have sworn that being strongly typed meant that the type of an object could not change once created and could not, without conversion, be treated as a different type (e.g. not being able to just pass any arbitrary values to functions in Python). I guess I was confused about what strongly typed meant, though I'm not sure how else to describe what I mean.

As for whether it's doable in Java, unfortunately it is not, due to the nature of how classes are loaded in Java (they're not initialized until they are referenced by code). This means that you would still have to fall back to archaic practices such as static factories/all-knowing registrators.

@rapidcoder Besides the horrifying syntax required to pull it off along with all the exception handling (even more horrifying), and the fact that it is not the compiler or implementation doing the work for you as they should, it is not suitable due to limitation in Java and how the JVM generally works. Once again this is a scenario where I'd love to use Java but because of the limitations and poor implementation I cannot.

I know it seems like I'm just completely rejecting all suggestions, but I am actually looking into these things, or already have. I have actually tried to set up various scenarios in e.g. Java, Python, C++, etc. and have run into a brick wall each time.

I guess I'm just tired of running into brick walls and running out of options and redesigning everything and rewriting code and scrapping entire class designs and redesigning, rewriting, running into a brick wall, trying another language, researching for days, running into more brick walls, etc. and the worst part is that I can't even describe what it is I need (as is clear from this thread and others where nobody knows what I am talking about). I feel useless, like I've amassed all this knowledge for various languages and designs and such and I can't even complete a simple project because I don't understand how to avoid running into brick walls.
@LB, strong/weak typing is a concept that doesn't have a particularly hard definition.
wikipedia wrote:
One of the more common definitions states that weakly typed programming languages are those that support either implicit type conversion (nearly all languages support at least one implicit type conversion), ad-hoc polymorphism (also known as overloading) or both.


Can you explain exactly what it is that you want to do? Maybe we could be of some help.

As for whether it's doable in Java, unfortunately it is not, due to the nature of how classes are loaded in Java (they're not initialized until they are referenced by code). This means that you would still have to fall back to archaic practices such as static factories/all-knowing registrators.


What does it have to do with classes being/not-being first-class?
What are you going to do?
Last edited on
I messed up pretty bad with this thread, I don't even know what I was looking for in my first post anymore. I just randomly thought of it when I was trying to find a middle ground between Java's fake generics and C++'s completely inline templates. Obviously it can't work in a statically typed language without being just syntactic sugar.

At least I learned not to confuse static and string typing.
Topic archived. No new replies allowed.