• Advertisement
Sign in to follow this  

Language curiosity

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm curious if a language I'm not familiar with [but others here might be] or perhaps a language I already know, but not well enough has a particular feature. Most interpreted languages, such as PHP and perl are not strongly typed, and allow variable definition pretty much anywhere. Similarly to how you can add "members" to a perl hash [which isn't really expanding the object, just adding a new named member to a hash table...] I wonder if other languages allow this sort of additive construction of structures/classes/objects, without inheritance? Basically being able to declare extra member functions/variables outside of the main declaration, due to an optional include or interface being available. In C++ this sort of scenario is often solved by friend functions [think the common cout friend functions], or by #ifdef's; which is far messier than it should be it seems.

Share this post


Link to post
Share on other sites
Advertisement
If I understand you correctly you're asking if other languages let you do:


// psuedocode
class SomeClass
{
someMember
otherMember
}

SomeClass sc;

sc.newMember = something;


If that's what you're asking, Python allows this (but I hate code that uses it)

Share this post


Link to post
Share on other sites
In javascript you can do things like that.

x = {foo: "asdf", bar: {baz: 4, array: [0, 1, 2, 3]}};
print(x.foo);
x.bar.baz += x.bar.array[3];
x["bar"] = x["bar"]["baz"];

Or something, it's been a while since I've used javascript.

Note: javascript != Java

Why is javascript getting changed to lowercase?

Share this post


Link to post
Share on other sites
If you require that behaviour in a language that doesn't provide it "natively", you can always make use of an appropriate data structure. For example, in C++ you can acheive a limited measure of 'dynamic typing' by representing things as some generic root Object class, which in turn wraps std::map<string, Object*>.

Share this post


Link to post
Share on other sites
Eh, sort of. More like this though:


some_header.h

class something{
// stuff
};

network.h

// optional networking code

something::net_send(){
// some new optional interface for something
// can be nicely excluded on builds for non-networked
// hosts
//
// and does not require inclusion trickery to ensure network.h
// interfaces are visible to this implimentation.
}


This is perhaps a bad example, as this sort of thing can be done fairly easily with templating or a #ifdef _network_ block in the something declaration...

Hrm. Something like that though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement