Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 1494 times)
0 Members and 1 Guest are viewing this topic.
Online Abuse

JGO Knight


Medals: 15


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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline ra4king

JGO Kernel


Medals: 356
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

Online Abuse

JGO Knight


Medals: 15


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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
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
Offline ra4king

JGO Kernel


Medals: 356
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
« League of Dukes »

« JGO Overlord »


Medals: 840
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
Offline ra4king

JGO Kernel


Medals: 356
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: 366
Projects: 2
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.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!