Projects

Ticket #47 (new defect)

Opened 4 years ago

Last modified 3 years ago

Cannot pass a :symbol directly as a named parameter

Reported by: cremes@… Owned by: lsansonetti@…
Priority: minor Milestone:
Component: MacRuby Keywords: symbol named keyed parameter
Cc:

Description

Here's an example to illustrate. Code was tested against the "testing" branch from 20080405.

class A

def initialize

@h = {}

end

def setValue(val, forKey:key)

@h[key] = val

end

end

a = A.new

a.setValue("triple", forKey::three) # fails

a.setValue("triple", forKey: :three) # succeeds

three = :three

a.setValue("triple", forKey:three) # succeeds

a.setValue("triple", forKey:'three'.to_sym) # succeeds

When using the #:: syntax the parser mistakenly views it as an attempt to access the method #forKey on Object. At minimum it should probably throw a more useful error message and perhaps suggest adding a space between colons.

Attachments

symbol_error.diff Download (0.5 KB) - added by dj2@… 4 years ago.

Change History

Changed 4 years ago by lsansonetti@…

Currently, a.setValue("triple", forKey::three produces undefined local variable or method `forKey' for main:Object (NameError).

I wonder if we should do something. forKey::three is a perfectly valid syntax (it basically means forKey.three).

>> 123.to_s
=> "123"
>> 123::to_s
=> "123"

In that case it would mean: send #setValue with 2 regular (not-named) arguments: "triple" and the result of forKey::three.

Changed 4 years ago by cremes@…

I agree the syntax is valid but it does confuse the issue for named parameters. Right now the warning is a bit too arcane. If it printed what it does now *in addition to* an extra sentence regarding named parameters and how to use them properly, then I think it would be an improvement.

Thanks for working on this. I'm doing a presentation on it for the Chicago Cocoa user's group tomorrow night and ran across this while putting my slideshow together.

Changed 4 years ago by lsansonetti@…

I do agree that we need better error reporting in this case, let's try to fix that.

Good to know that you're presenting about it, good luck :-)

Changed 4 years ago by lsansonetti@…

  • milestone changed from MacRuby 0.2 to MacRuby 1.0

Changed 4 years ago by dj2@…

Changed 4 years ago by dj2@…

The attached symbol_error.diff is a possible fix for this. I don't see any way in the code to be able to print the symbol name into the error message instead of forKey (attempting to add another parameter to the args array causes other issues as that's used in the method_missing dispatch I believe). This at least gives the user an idea of what could be wrong.

Changed 3 years ago by anonymous

  • milestone MacRuby 1.0 deleted

Milestone MacRuby 1.0 deleted

Note: See TracTickets for help on using tickets.