Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
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 2327 times)
0 Members and 1 Guest are viewing this topic.
Offline Abuse

JGO Ninja


Medals: 50


falling into the abyss of reality


« Posted 2012-10-18 17: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.
Offline ra4king

JGO Kernel


Medals: 438
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-10-18 21: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 Ninja


Medals: 50


falling into the abyss of reality


« Reply #2 - Posted 2012-10-18 21: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1238
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-10-18 22: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!
Offline ra4king

JGO Kernel


Medals: 438
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-10-18 22:40:43 »

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

Offline Riven
Administrator

« JGO Overlord »


Medals: 1238
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-10-18 22: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!
Offline ra4king

JGO Kernel


Medals: 438
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-10-18 22: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-18 23: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

« JGO Bitwise Duke »


Medals: 829
Projects: 4
Exp: 8 years



« Reply #8 - Posted 2012-10-19 00: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.

 
roseslayer (473 views)
2016-08-06 11:43:29

roseslayer (430 views)
2016-08-06 09:43:11

xTheGamerCodes (507 views)
2016-08-04 15:40:59

xTheGamerCodes (500 views)
2016-08-04 15:40:24

orrenravid (848 views)
2016-07-16 03:57:23

theagentd (925 views)
2016-07-11 14:28:54

Hydroque (1021 views)
2016-07-06 05:56:57

Hydroque (1017 views)
2016-07-03 08:52:54

GrandCastle (832 views)
2016-07-01 09:13:47

GrandCastle (640 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!