Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Character could (should?!) implement CharSequence?  (Read 868 times)
0 Members and 1 Guest are viewing this topic.
Offline Abuse

JGO Coder


Medals: 2


falling into the abyss of reality


« Posted 2012-10-18 19:36:54 »

I came across this when helping a noobie understand why this doesn't compile:

1  
someString.replace('A',"BB")


It got me thinking; why doesn't the Character class implement CharSequence.
Logically a Character is a sequence of 1, thus there's no reason it shouldn't implement it.

Doing so would have interesting consequence when mixed with autoboxing.

Thanks to autoboxing, the uncompilable code above would start compiling. (char autoboxes to Character, which would implement CharSequence & thus meet the contract of replace(CharSequence,CharSequence))

I'm obviously not advocating such a change, as it'd introduce all sorts of potential confusion as to precisely what method was being invoked, and possible performance problems in the event of typos.

Still, it was something I hadn't noticed or considered before.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2012-10-18 23:21:35 »

Because a Character cannot somehow implement the required methods? Wink

Stop being lazy and just replace the single quotes with double quotes Tongue

Offline Abuse

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #2 - Posted 2012-10-18 23:33:29 »

Because a Character cannot somehow implement the required methods? Wink

Seems pretty trivial to implement IMO.

Quote
Stop being lazy and just replace the single quotes with double quotes Tongue

hehe, true.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2012-10-19 00:02:25 »

Because a Character cannot somehow implement the required methods? Wink
Hm... with that reasoning, it would also not support strings with a length of one.

Anyway, I think this is genuinly overlooked by the Sun engineers, when they added CharSequence.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #4 - Posted 2012-10-19 00:40:43 »

I was thinking more of "charAt" and "subsequence". Those two would seem out of place :S

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #5 - Posted 2012-10-19 00:42:03 »

just as out of place as for a string of length one - would you also have problems imagining charAt for a string of length zero?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #6 - Posted 2012-10-19 00:52:47 »

Those are scenarios for Strings. Strings can be of any size. A Character will always have a length of 1 so those two methods are useless.

Offline davedes
« Reply #7 - Posted 2012-10-19 01:00:56 »

The methods wouldn't be useless since they would be used by replace() and other functions which don't know anything about Character or String or what have you. That's the whole point of interfaces... Smiley


Offline theagentd
« Reply #8 - Posted 2012-10-19 02:33:10 »

We're trying to make games and we can't even get our chars right? Go us.  Tongue

EDIT: The only "conflict" I can find in the String API is for replace():
1  
2  
replace(char oldChar, char newChar)
replace(CharSequence target, CharSequence replacement)

I don't think that would be a problem though? It should prioritize the char version if the input is char even if it extends CharSequence. Besides, the first one would just be an optimization of the CharSequence one specifically for chars.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (73 views)
2013-05-17 21:29:12

alaslipknot (85 views)
2013-05-16 21:24:48

gouessej (114 views)
2013-05-16 00:53:38

gouessej (109 views)
2013-05-16 00:17:58

theagentd (120 views)
2013-05-15 15:01:13

theagentd (109 views)
2013-05-15 15:00:54

StreetDoggy (154 views)
2013-05-14 15:56:26

kutucuk (176 views)
2013-05-12 17:10:36

kutucuk (173 views)
2013-05-12 15:36:09

UnluckyDevil (182 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.102 seconds with 21 queries.