Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 269 270 [271] 272 273 ... 796

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

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4050 on: March 01, 2013, 05:46:27 pm »

woah! calm down there cowboy, yes that may look like a good idea at first, but it includes ALL of the function names from that namespace, so what if you wanted to use 2 namespaces that both had a function with the same name? It wouldn't know what to do with itself, and probably wouldn't do what you want it to.

Doesn't the compiler warn you when you do that? Because it kind of seems to me like it should. "Hey dude you got like six different functions named 'bluh()' available in this context which one do you want me to use here?"

Yeah, it probably would,


Actually, if the signatures are different (the int, int or void things), it just overloads them no problem. O.o

Note: not sure what they are called...

thats called Overloaded fuctions.  :P

and here's a basic lesson about them, for anyone that doesn't know what that is.

You can have functions with the same name available in a scope, if each function has a different parameter list.

int foo(int x)
     {return x;}

int foo(int x, int y)
     {return x+y;}

so it uses the function based on how many parameters are in your function call.

foo(5); would use one, and
foo(5,5); would use the other.


This isn't the only time the parameter list in the function call might be different than the parameter list for the definition.
you also have the option of using default parameters, which are used in place of one included in the function call.

int foo(int x, int y = 5)
      {return x+y)

then foo(5); and foo(5,5) would both result in the same thing.


but.. I'm not sure what would happen if you included a 2 param funtion with one default parameter, and a function with the same name and only 1 parameter, because then calling it would call both functions. 
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4051 on: March 01, 2013, 06:01:36 pm »

No, I mean I don't know if (int, int) is called a signature or not. You can see I specificallysaid "overload", so I probably know what an overloaded function is :P

The default behavious is the compiler complaining about ambiguous functions, I believe.

@MorelyDev: I find myself using the ::s instead of namespaces for most libraries, actually. I suppose it won't be too hard to extend that to the std namespace...
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4052 on: March 01, 2013, 06:24:23 pm »

Common Lisp allows you to import only specific keywords from one package into another, using :import-from. You can also assign them a different name using :shadowing-import-from. Doesn't C++ have a similar system?
Logged

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4053 on: March 01, 2013, 07:13:13 pm »

Common Lisp allows you to import only specific keywords from one package into another, using :import-from. You can also assign them a different name using :shadowing-import-from. Doesn't C++ have a similar system?

Yes (mostly).

Code: (Exposing a single function) [Select]
#include <iostream>

using std::cout;

int main(){
    cout << "C++ is awesoem" << std::endl;
}

Code: (Namespace aliases) [Select]
#include <iostream>

namespace Standard = std;

int main(){
    Standard::cout << "C++ is aewosem" << Standard::endl;
}

I don't think you can alias a function, though.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4054 on: March 01, 2013, 07:26:57 pm »

Common Lisp allows you to import only specific keywords from one package into another, using :import-from. You can also assign them a different name using :shadowing-import-from. Doesn't C++ have a similar system?
My god that is sexy. Somebody write to microsoft! C# is missing functionality!
I mean you can import a class under an alias, but keywords? How am I not doing that right now?

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4055 on: March 01, 2013, 09:51:29 pm »

Common Lisp allows you to import only specific keywords from one package into another, using :import-from. You can also assign them a different name using :shadowing-import-from. Doesn't C++ have a similar system?
My god that is sexy. Somebody write to microsoft! C# is missing functionality!
I mean you can import a class under an alias, but keywords? How am I not doing that right now?
I don't know if it's that easy to translate since keywords in CL are essentially a special type of symbol. Technically, a keyword in CL is a symbol prefixed with a semicolon and unrelated to keywords in C#. I really should've used the word symbol in my previous post. Regardless of that, you can also import macros from one namespace into another, which would be the equivalent of C#'s keywords or C's preprocessor macros. The reason that this works, while many other languages don't allow it, has to do with the fact that CL does multiple compilation passes in which macros are expanded into code stepwise, so the compiler can postpone evaluation of a macro to a later pass if needed. I'm not sure if other preprocessors are that sophisticated.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4056 on: March 01, 2013, 10:53:49 pm »

I don't think you can alias a function, though.

You sort of can using preprocessor directives.

Code: [Select]
#include <iostream>
#include <algorithm>

#define MrCount std::count

int main(){
    int MrList[] = {1,2,2,3,4,6,5,3,5};

    std::cout << "How many 2's:" << std::count(MrList, MrList+9, 2) << std::endl;
    std::cout << "How many 5's:" << MrCount(MrList, MrList+9, 2)    << std::endl;
    return 0;
}
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4057 on: March 02, 2013, 02:54:12 am »

Quick lesson on function/method signatures!

Applies to: C++ and Java

I'm going to be using the word function here, but you can replace it with method if you so please.

There are four essential parts to a function signature: the return type, the name, the parameter list, and the scope.

Code: [Select]
// Format for a function: <return type> <method name> (<parameters>)
// Valid examples (method bodies excluded because they're irrelevant)
int foo();
void bar(int a);
char bar(int a);
int foo(int b);

namespace foobar { // C++ only
    int foo(int b);
}

class AClass {
    char bar(int a);
}; // no semicolon for Java

As long as at least one of the essential parts differ, overloading works.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4058 on: March 02, 2013, 03:47:05 am »

I think the function signature does not depend on the return type in C++.

void bar(int a);
char bar(int a);
int bar(int a);


-> error: new declaration 'char bar(int)'
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4059 on: March 02, 2013, 03:51:52 am »

I think the function signature does not depend on the return type in C++.

void bar(int a);
char bar(int a);
int bar(int a);


-> error: new declaration 'char bar(int)'
It still is part of the signature, but it's illegal to define signatures that are identical save for the return type. Because of this:

bar(3); // <compiler> WTFBBQ
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4060 on: March 02, 2013, 10:40:54 am »

I think the function signature does not depend on the return type in C++.
Well, it does, but it's still arbitrary as you don't have to use the return type. Implicitly castable types also make it arbitrary. For instance: bar(char) and bar(int) are arbitrary, as char implicitly casts to int, so if you do a bar('a'), it won't know if you're doing a char or an int.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4061 on: March 02, 2013, 02:04:13 pm »

The prefix's add clarity, and readability is an important thing in coding.
I beg to differ. The prefix to me adds bloat that makes reading the meaningful part of the code harder. If you're careful, using "using" when appropriate is perfectly fine.

Yes (mostly).

Code: (Exposing a single function) [Select]
#include <iostream>

using std::cout;

int main(){
    cout << "C++ is awesoem" << std::endl;
}

Code: (Namespace aliases) [Select]
#include <iostream>

namespace Standard = std;

int main(){
    Standard::cout << "C++ is aewosem" << Standard::endl;
}

I don't think you can alias a function, though.
cout is technically an object, but that doesn't change the fact that you can do the same thing with functions. Also, there are multiple ways to alias functions in C++: function pointers, function references, inline wrappers, etc.

Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4062 on: March 02, 2013, 02:31:27 pm »

I think the function signature does not depend on the return type in C++.
Well, it does, but it's still arbitrary as you don't have to use the return type. Implicitly castable types also make it arbitrary. For instance: bar(char) and bar(int) are arbitrary, as char implicitly casts to int, so if you do a bar('a'), it won't know if you're doing a char or an int.

Ahh, yes, you're right. I forgot about implicit casting. I've been using too much Java recently.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4063 on: March 05, 2013, 12:53:46 am »

Remember that platformer I made? Here's the source. The OpenGL calls are slightly scrambled (this was a bottom-up project), but should be easy to read.
Where, might I ask, did you get your texture decoder? It seems your project was missing two libraries, lwjgl, which I already had lying about, and what ever is doing all dem preddy pictures.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4064 on: March 05, 2013, 12:56:36 am »

So I'm learning assembly for MARIE (a Machine Architecture that is Really Intuitive and Easy; exactly what it says on the tin) through my Computer Architecture and Organization course. I wrote a simple adder, and it worked perfectly. I cannot express my pride in words alone.
Pages: 1 ... 269 270 [271] 272 273 ... 796