Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 97 98 [99] 100 101 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 883183 times)

nenjin

  • Bay Watcher
  • Inscrubtable Exhortations of the Soul
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1470 on: February 15, 2012, 11:04:00 am »

Chalk it up to magical elves.
Logged
Cautivo del Milagro seamos, Penitente.
Quote from: Viktor Frankl
When we are no longer able to change a situation, we are challenged to change ourselves.
Quote from: Sindain
Its kinda silly to complain that a friendly NPC isn't a well designed boss fight.
Quote from: Eric Blank
How will I cheese now assholes?
Quote from: MrRoboto75
Always spaghetti, never forghetti

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1471 on: February 15, 2012, 11:15:39 am »

Question: Is there a practical value behind overloading aside from rendering your code an unreadable mess? We just got to operator overloading and I really don't see a use for it other than pranking someone or ensuring job security.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

nenjin

  • Bay Watcher
  • Inscrubtable Exhortations of the Soul
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1472 on: February 15, 2012, 11:20:31 am »

I asked the same question when I learned about them.

Basically, if you're going to call some operator frequently and you want to compact it down with a couple other statements, you'd overload it.

I think its application is mostly for smaller, single-task focused programs. If you're writing your average game or something, I don't think you'd find yourself wanting to overload commonly used operators because you'll need to use them in a variety of ways.
Logged
Cautivo del Milagro seamos, Penitente.
Quote from: Viktor Frankl
When we are no longer able to change a situation, we are challenged to change ourselves.
Quote from: Sindain
Its kinda silly to complain that a friendly NPC isn't a well designed boss fight.
Quote from: Eric Blank
How will I cheese now assholes?
Quote from: MrRoboto75
Always spaghetti, never forghetti

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1473 on: February 15, 2012, 11:21:59 am »

I've completely forgotten what overloading means in this way. Can someone explain it for me?
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1474 on: February 15, 2012, 11:23:10 am »

Operator overloading (as it was explained to my class) is taking commonly used operators (+,=,<<, etc) and giving them custom definitions.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1475 on: February 15, 2012, 11:31:37 am »

Ah. I mean, of course that's useful! I mean, a lot of its done at a more base level than your average programmer needs, but if I don't override their definition my "permissions" class isn't going to even know what it means when you say "user.is_allowed_in?(permissions_list_1 + permissions_list_2)"

Although... I guess it really isn't overloading when I do it, since I do it almost exclusively in situations where they aren't defined...

Ah, wait! I thought of an example! I sometimes use it for error-handling and validation, where I pass it on to the /actual/ method after making sure everything checks out.

I can't imagine it being nearly as useful in those languages where those operators are global methods, though. That sounds messy.
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1476 on: February 15, 2012, 11:32:21 am »

I do believe that what you just said is way over my head.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1477 on: February 15, 2012, 11:35:01 am »

Well, it's like this. Say you want to make your own custom array class for some reason - maybe it does something special. It's not terribly uncommon.

You need to define "<<" to work for that class, correctly, assuming you don't inherit it's definition from array, right? Even if you DO inherit from array, it may need some sort of validation (say - you're only allowed to add objects of a certain type to this special type of array), so you can override that bit to add some validation before continuing as normal.
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1478 on: February 15, 2012, 11:36:38 am »

And that only happens in the specific function or whatever you need it to, so that "<<" works as normal in the rest of the program?
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #1479 on: February 15, 2012, 11:37:20 am »

I've completely forgotten what overloading means in this way. Can someone explain it for me?

Operator overloading (as it was explained to my class) is taking commonly used operators (+,=,<<, etc) and giving them custom definitions.

You can also overload methods and members when creating a class that inherits from a non-abstract superclass.

This redefines the methods and members.

One thing to be careful of is how your language deals with this. Some languages will use the superclass method if the object is cast as the superclass and the new method if it is cast as its actual class. Some languages wont.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #1480 on: February 15, 2012, 11:39:42 am »

And that only happens in the specific function or whatever you need it to, so that "<<" works as normal in the rest of the program?
It only works with the class you overload it for, of course. Otherwise it would be useful for nothing but code obfuscation.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1481 on: February 15, 2012, 11:39:55 am »

Right. I'd probably save the original definition somewhere with a different name so I could call that after the validation.

And obviously there's other ways to do that sort of stuff, its just a possibility.

On the other hand, I work in a language where there is no difference between operator overloading and method/function overloading, since operators are just methods like any other method. (Forgot what language you're working with, don't know if its the same for you)

But generally it is used when you're inheriting from something and your object doesn't work quite the same as its parent in some way.

Anyways, OTHER sorts of overloading are pretty common, aren't they? To add additional stuff for the child then make a super call to pass it to the parent as normal?
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1482 on: February 15, 2012, 12:04:05 pm »

Hey... new stupid question.

I want to take a dictionary and return the key that maps to the minimal value.  Any of y'all know how to do that with built-in functions?

Oh wait, I found a way to do this.  Turns out python has a filter() function that makes it easier.

Code: [Select]
dict = {'a': 1, 'b': 2, 'c': 1, 'd': 5, 'e':2, 'f':1}
keys = map(lambda x: x[0], filter(lambda x: x[1] == min(dict.items()[1]), dict.items()))
print keys

Its a bit hard to read, but it does the trick.  the filter method returns a subset of a list for where the lambda function is true, and the map function I'm using to just return the keys, instead of both keys and values.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1483 on: February 15, 2012, 12:54:51 pm »

Operator overloading is particularly useful for creating classes for common mathematical constructs. For example, if you create a class to represent a mathematical vector, you may want to override * in the vector class to do piece wise multiplication against other instances of the vector class as well as scalar multiplication. Without overrides, your code would look more like
Vector3 = Vector1.Multiply(Vector2);
Vector2 = Vector1.Multiply(scalar);
or worse (if not implemented as a function at all), instead of the more clean
Vector3 = Vector1 * Vector2;
Vector2 = Vector1 * scalar;
Logged

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1484 on: February 15, 2012, 01:03:06 pm »

Yeah, I managed to do it with a "for" loop, and it worked just fine.  Thankfully.

Now I just have to do some very minor bugfixing and the project will be submission-ready.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".
Pages: 1 ... 97 98 [99] 100 101 ... 796