4 ms·
PHP to Objective C, where are the parameters?
- dqminh 15y agopython also has this right by introducing named parameters. The same function in python can be written as: do_send_mail(recipient="some@email.com", cc="another@email.com", subject="Hello!", body="hi!")
- kentnguyen 15y agothanks for the point. I have no background in Python so I didn't take Python into consideration. In my opinion, there are more people like me: coming from pure HTML/CSS/PHP background straight to Objective C
- rbanffy 15y agoLook into Smalltalk (which is the language Obj-C borrowed the message syntax from) too. Squeak by Example is an excellent intro. http://squeakbyexample.org/ http://squeakbyexample.org/
- masklinn 15y agoThere are significant differences though: 1. It's not possible to mandate the usage of keyword attributes in Python 2, this function could be called with do_send_mail("some@email.com", "another@email.com", "Hello!", "hi!") JS/Ruby's low-fi version of using hashes actually helps there as you have to provide hash arguments via the hash. Python 3 lets you fix this issue (a great reason to switch, by the way) by using a ⭑[0] argument (not a ⭑-arg, which you can use in Python 2 but which will simply make your stuff fall into a black hole unless the creator of the function asserts no positional argument was passed via e.g. `assert not arg`; furthermore Python 2 would require that a default value — to check for — be given for each keyword argument): >>> def foo(*, a, b): ... print(a, b) ... >>> foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() needs keyword-only argument a >>> foo(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() takes exactly 0 positional arguments (2 given) >>> foo(a=1, b=2) 1 2 2. the arguments are separate from the method name in Python, in your example the method is `do_send_mail`, in Smalltalk and Objective-C it's `doSendMailToRecipient:cc:subject:body:` which has advantages and inconvenients. Python is easier for default values (you need a new method in Smalltalk or Obj-C), but the Smalltalk/Obj-C method is simpler for behavioral differences (as the dispatch is done at callsite, Python will need internal soup) 3. There is no order in Python's argument (whereas changing the order in Smalltalk/Obj-C changes the method called), which means callers can lower the call's readability by swapping arguments around in... less than sensible ways. (nb: I love Python, don't interpret my comment as a put down of it, just pointing out fundamental and important difference between Python's keyword arguments and Smalltalk's keyword messages) [0] I hate HN's markup.
- nknight 15y ago> It's not possible to mandate the usage of keyword attributes in Python 2 That's not completely true, but it's inconvenient. If you're prepared to go the kwargs route, calling with positional arguments raises a TypeError: >>> def foo(**kwargs): ... print(kwargs['a'], kwargs['b']) ... >>> foo(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() takes exactly 0 arguments (2 given) >>> foo(a=1, b=2) (1, 2) You have to manually check for required parameters, though, and assign any necessary defaults.
- masklinn 15y ago> That's not completely true, but it's inconvenient. If you're prepared to go the kwargs route Ah true, I'd not thought about using kwargs only. > You have to manually check for required parameters, though, and assign any necessary defaults. Yeah, and you likely lose parameters management in the IDE.
- RandallBrown 15y agoI'm going to have to start referring to this post when I explain why I like objective c. Usually people just give me this crazy stare like I'm some kind of moron. One thing though, if you spell a method name wrong, it will still compile. It's only a warning that "the object may not respond to that selector". Of course if you have warnings as errors turned on, then of course it will error.
- kentnguyen 15y agoi guess im abit overboard trying to make my point. but you are right, no error, just warnings.
- masklinn 15y ago> One thing though, if you spell a method name wrong, it will still compile. It's only a warning that "the object may not respond to that selector". And it's important to understand that: while Objective-C's "C" part is statically typed, Objective-C's objective model comes from Smalltalk and remains essentially dynamically typed. That's in fact what `id` is: a purely dynamic type. This is quite unlike Java or OCaml.
- mmariani 15y agoNot a bad intro to ObjC message syntax. Even though the article has few mistakes. Most notably ObjC syntax does not have functions that are called, it has messages that are sent to objects and classes. In the end all messages are "translated" to C function calls to objc_msgSend(). Everything makes sense after you read the proper documentation. Which in this case is http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/ObjCRuntimeRef.pdf http://developer.apple.com/library/mac/documentation/Cocoa/R...
- kentnguyen 15y agoThanks for pointing that out, nevertheless I guess using the similar concept to PHP/JS makes the user relate to the transition better. I did read that Apple doc when I first starting out but it was overwhelming to me at that time and it backfired. I was more discourage to learn Objective C after seeing the doc.
- RandallBrown 15y agoI've always found it easier to just explain the messages by calling them functions, otherwise people will often get wrapped up in their previous conceptions of messages in programming.
- mmariani 15y agoNot a bad intro to ObjC message syntax. Even though the article has few mistakes. Most notably ObjC syntax does not have functions that are called, it has messages that are sent to objects and classes. In the end all messages are "translated" to C function calls to objc_msgSend(). Everything makes sense after you read the proper documentation. Which in this case is http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/ObjCRuntimeRef.pdf http://developer.apple.com/library/mac/documentation/Cocoa/R...
- mattmanser 15y agoI hope you understood the very fundamental reason why Objective-C syntax has to be the way it is. It does not need to be like this in a statically typed language. Most statically typed languages don't require you to pass the parameter name. Some have this optionally so you can mix and match or have default values (C# for example http://msdn.microsoft.com/en-us/library/dd264739.aspx http://msdn.microsoft.com/en-us/library/dd264739.aspx). If you'd written other statically typed language the Obj-C syntax would probably still be surprising to you. Brilliant design by Apple it is not, it's the nature of statically typed languages. For example your comment: Now, which one is the recipient? What is the subject? Shows a lack of experience with statically typed languages. IDEs will tell you if you hover over the method with your mouse as well as when you're creating the method (including all the variations). On the other hand it's quite fun to see someone naturally find out some of the reasons why some people prefer statically typed languages.
- masklinn 15y agoI'm sorry, but all of your comment is nonsense. 1. Static typing does not help with writability when you're passing 4 strings to a method (now that can be made into a debate over good usage of types, language cultures and many other things, point is when your arguments are "untyped" having a stricter type checking of nothing will not help you) 2. Static typing does not help with readability when you end up with do_send_mail("some@email.com", "another@email.com", "Hello!", "hi!") anyway, there are many cases where keyword arguments significantly enhance the reading experience 3. IDEs are perfectly able to "tell you if you hover over the method with your mouse as well as when you're creating the method" for dynamically languages. The only thing a statically typed language will help you with TFA's example is if you have extraneous or missing arguments, and there even a good IDE will do it in a dynamically typed language (Jetbrains's IDEs do, for instance).
- mattmanser 15y agoDid you read the article? The author claims it has to be this way in Obj-C. Not that's it's a nice thing. It doesn't at all. The Objective-C function’s name is designed to include the parameters’ names. This is not the case with most web programming languages. Why does it have to be this way? The obvious reason is Objective-C is a compiled language, once you get it wrong, it cannot even be compiled. The whole premise of the article is fundamentally flawed. I was just trying to tell him in a nice way. All his 'reasons' are bunk when you look at what a static language provides you in terms of explicitness. I'll leave with this: When I was still coding in PHP, I found myself constantly having another file open just see the declaration of a function I wrote before that That never happens in a static language. It does in dynamic. Or perhaps he just doesn't use IDEs and that's the actual revelation?
- gerggerg 15y agoTotally personal preference. I would rather have a language be concise and the documentation verbose than the other way around or with both verbose. Having it be verbose is great for learning, but after a short while syntax like: do_send_email(recipient, cc, subject, body) is much better for quickly reading code. Plus, reading the docs is hugely important. Having to check every now and then is great for getting you back into them.
- kentnguyen 15y agobefore I continue. Have you done any development in Objective C?
- gerggerg 15y agoVery minimal.
- roadnottaken 15y agoI was interested in this article when I saw that you "... wanted to help out those who began to learn iOS evelopment, especially programmers who are coming from no programming background or web development background" but then the first line of your next article is: "I assume you are very familiar with declaring functions in [Javascript, PHP, or Ruby], if not, you should not be reading this" So... which one is it? A tutorial for non-programmers or programmers familiar with other languages? or a tutorial specifically for people fluent in Javascript/PHP/Ruby?